在 Ubuntu (14.04) VM 上,我有一个简单的节点服务器,我想在启动时运行它。以下脚本在调用时可以正常工作./noderoot start
,但不会在启动时运行。该脚本是
#!/bin/sh
### BEGIN INIT INFO
# Provides: noderoot
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: bootin and rootin
# Description: Start root server on boot
### END INIT INFO
export PATH=$PATH:/usr/bin
export NODE_PATH=$NODE_PATH:/home/azureuser/npm/bin
case "$1" in
start)
forever --sourceDir=/var/www/root -p /var/www/logs app.js
;;
stop)
forever stop --sourceDir=/var/www/root app.js
;;
*)
echo "Usage: /etc/init.d/noderoot {start|stop}"
exit 1
;;
esac
exit 0
此脚本位于/etc/init.d
权限755
内。我尝试运行,update-rc.d
结果如下:
root@linux-dev:~# update-rc.d noderoot defaults 80
Adding system startup for /etc/init.d/noderoot ...
/etc/rc0.d/K80noderoot -> ../init.d/noderoot
/etc/rc1.d/K80noderoot -> ../init.d/noderoot
/etc/rc6.d/K80noderoot -> ../init.d/noderoot
/etc/rc2.d/S80noderoot -> ../init.d/noderoot
/etc/rc3.d/S80noderoot -> ../init.d/noderoot
/etc/rc4.d/S80noderoot -> ../init.d/noderoot
/etc/rc5.d/S80noderoot -> ../init.d/noderoot
root@linux-dev:~# update-rc.d noderoot enable
update-rc.d: warning: start runlevel arguments (none) do not match noderoot Default-Start values (2 3 4 5)
update-rc.d: warning: stop runlevel arguments (none) do not match noderoot Default-Stop values (0 1 6)
Enabling system startup links for /etc/init.d/noderoot ...
Removing any system startup links for /etc/init.d/noderoot ...
/etc/rc0.d/K80noderoot
/etc/rc1.d/K80noderoot
/etc/rc2.d/S80noderoot
/etc/rc3.d/S80noderoot
/etc/rc4.d/S80noderoot
/etc/rc5.d/S80noderoot
/etc/rc6.d/K80noderoot
Adding system startup for /etc/init.d/noderoot ...
/etc/rc0.d/K80noderoot -> ../init.d/noderoot
/etc/rc1.d/K80noderoot -> ../init.d/noderoot
/etc/rc6.d/K80noderoot -> ../init.d/noderoot
/etc/rc2.d/S80noderoot -> ../init.d/noderoot
/etc/rc3.d/S80noderoot -> ../init.d/noderoot
/etc/rc4.d/S80noderoot -> ../init.d/noderoot
/etc/rc5.d/S80noderoot -> ../init.d/noderoot
据我所知,此操作已成功完成。我可以通过运行 来启动服务sudo service noderoot start
。但是,机器重新启动时我的可执行文件未运行。有没有办法解决发生的问题?是否有特定的日志文件可以说明如何修复此问题?
答案1
查看/var/log/boot.log
(Letizia 提供的位置)的日志,我发现了一行非常可疑的内容:
/etc/rc2.d/S80noderoot: 16: /etc/rc2.d/S80noderoot: forever: not found
找不到命令表明这是路径问题。果然,
export PATH=$PATH:/usr/bin
export NODE_PATH=$NODE_PATH:/home/azureuser/npm/bin
被逆转了。切换这些变量以指向正确的目录,以及cd
在执行之前向脚本添加forever
,似乎已经解决了运行脚本遇到的所有问题。