Warm tip: This article is reproduced from serverfault.com, please click

linux-使用 rc.local 运行脚本:脚本有效,但在启动时无效

(linux - Run script with rc.local: script works, but not at boot)

发布于 2011-10-16 09:10:07

我有一个 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 不起作用。怎么了?

Questioner
Jurian Sluiman
Viewed
0
Jurian Sluiman 2011-12-01 03:05:52

我最终得到了upstart,效果很好。