我有一个 node.js 脚本,它需要在启动时启动 在 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
关于linux - 使用 rc.local : script works, 运行脚本,但不在启动时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7783341/