我有一个 node.js 脚本,它需要在启动时启动并在 www-data 用户下运行。在开发过程中,我总是以以下方式启动脚本:
su www-data -c 'node /var/www/php-jobs/manager.js
我确切地看到了发生的事情,manager.js 现在工作得很好。搜索所以我发现我必须把它放在我的/etc/rc.local. 此外,我学会了将输出指向一个日志文件并将其附加2>&1到“将标准错误重定向到标准输出”,它应该是一个守护进程,所以最后一个字符是&.
最后,我的/etc/rc.local样子是这样的:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
su www-data -c 'node /var/www/php-jobs/manager.js >> /var/log/php-jobs.log 2>&1 &'
exit 0
如果我自己运行它(sudo /etc/rc.local):是的,它有效!但是,如果我执行重新启动,则没有node进程正在运行,/var/log/php-jobs.log则不存在,因此 manager.js 不起作用。怎么了?