我有一个需要在启动时启动的 node.js 脚本and在 www-data 用户下运行。在开发过程中,我总是以以下方式开始脚本:
su www-data -c 'node /var/www/php-jobs/manager.js
我确切地看到发生了什么,manager.js 现在工作得很好。搜索所以我发现我必须把它放在我的/etc/rc.local
。另外,我学会了将输出指向日志文件并附加2>&1
“将 stderr 重定向到 stdout”,它应该是一个守护进程,所以最后一个字符是&
.
最后,我的/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 不起作用。怎么了?
在这个 rc.local 脚本示例中,我在执行的第一行使用 io 重定向到我自己的日志文件:
#!/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.
exec 1>/tmp/rc.local.log 2>&1 # send stdout and stderr from rc.local to a log file
set -x # tell sh to display commands before execution
/opt/stuff/somefancy.error.script.sh
exit 0
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)