我已经尝试在 /etc/rc.local
中添加永久启动代码,但没有成功。
当我在 /etc/rc.local
中使用 @reboot
关键字时,它说找不到 @reboot
。
所以我又开始使用 crontab
这是我的 crontab 脚本。除了重新启动之外,所有其他 crontab 都在工作。在 syslog
中,它说
Jun 4 09:51:12 ip-172-31-28-35 /usr/sbin/irqbalance: Balancing is ineffective on systems with a single cache domain. Shutting down
Jun 4 09:51:12 ip-172-31-28-35 cron[959]: (CRON) STARTUP (fork ok)
Jun 4 09:51:12 ip-172-31-28-35 cron[959]: (CRON) INFO (Running @reboot jobs)
Jun 4 09:51:12 ip-172-31-28-35 CRON[1005]: (ubuntu) CMD (/usr/bin/sudo -u ubuntu /usr/local/bin/forever start home/ubuntu/chat2/index.js)
这表明我的 cron 选项卡中的重启命令正在运行,但由于某种原因永远无法启动 Node 。重新启动后,我运行 forever list 并显示 No forever processes running
我假设问题出在 Node 和永远的路径上。我是新手,不知道在 crontab 中的这个语句中使用哪个确切路径。
我也试过以下方法:
@reboot /usr/local/bin/forever start -c /usr/local/bin/node /home/ubuntu/chat2/index.js
和
@reboot /usr/local/bin/forever start /home/ubuntu/chat2/index.js
这些都不起作用。
如果我运行 which forever
它说
/usr/local/bin/forever
如果我运行 which node
它说
/usr/local/bin/node
如果我得到我的 index.js 应用程序文件的完整路径,通过在我的 chat2 目录中执行 readlink -f index.js
它说
/home/ubuntu/chat2/index.js
我只想在每次系统重新启动时运行此命令。我想启动我的 Node 应用程序。当我手动 cd 到 chat2 目录时,以下行非常有效。我希望它能在重启时自动运行。
forever -m5000 -w start index.js
最佳答案
您可以使用您的代码而不是使用 cron 创建服务。实际上我更喜欢这样,因为您可以随时停止或启动它,也可以在系统重新启动或启动时运行它。
所以:
1- 在/etc/init.d/name_of_file 中创建一个服务
#!/bin/bash
#/etc/init.d/name_of_file
### BEGIN INIT INFO
# Provides: name
# Required-Start: $syslog
# Required-Stop: $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: add service
# Description:
#
### END INIT INFO
# Some things that run always
case "$1" in
start)
echo "Starting app_name "
touch /var/lock/app_name
cd /where/is/your/file
node index.js &
;;
stop)
echo " Stopping "
rm /var/lock/app_name
sudo pkill -f node
;;
status)
if [ -e /var/lock/app_name ]
then
echo "app_name is running"
else
echo "app_name is not running"
fi
;;
*)
echo "Usage:service app_name{start|stop|status}"
exit 1
;;
esac
exit 0
因此,您已经创建了一个服务来运行您的 nodejs 应用程序。
您必须授予该脚本运行权限
chmod +x /etc/init.d/app_name
现在您唯一要做的就是将其配置为在启动时运行。
运行:
sudo update-rc.d app_name defaults
然后每次您重新启动计算机时,该服务都会自行启动。
关于node.js - 无法在系统重新启动 Ubuntu Crontab 时启动 Node ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37628904/