Jur*_*man 38 linux bash ubuntu boot autorun
我有一个node.js脚本需要在启动时启动并在www-data用户下运行.在开发过程中,我始终使用以下命
su www-data -c 'node /var/www/php-jobs/manager.js
Run Code Online (Sandbox Code Playgroud)
我看到发生了什么,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
Run Code Online (Sandbox Code Playgroud)
如果我自己运行(sudo /etc/rc.local
):是的,它有效!但是,如果我执行重新启动没有node
进程正在运行,/var/log/php-jobs.log
则不存在,因此,manager.js不起作用.怎么了?
Joh*_*Doe 70
在这个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 2> /tmp/rc.local.log # send stderr from rc.local to a log file
exec 1>&2 # send stdout to the same log file
set -x # tell sh to display commands before execution
/opt/stuff/somefancy.error.script.sh
exit 0
Run Code Online (Sandbox Code Playgroud)
小智 10
在某些Linux(Centos&RH,例如)上,/etc/rc.local
最初只是一个符号链接/etc/rc.d/rc.local
.在这些系统上,如果符号链接断开,并且/etc/rc.local
是一个单独的文件,则/etc/rc.local
在启动时将无法看到更改- 启动过程将运行该版本/etc/rc.d
.(如果/etc/rc.local
手动运行,它们将工作,但不会在启动时运行.)
听起来像dimadima的系统,它们是单独的文件,但是/etc/rc.d/rc.local
调用/etc/rc.local
如果移动到备份目录并将其复制回来或从头开始创建它,那么从/etc/rc.local
"真实"到"真实" 的符号链接/etc/rc.d
可能会丢失rc.local
,而不会意识到原始的/etc
只是一个符号链接.
小智 5
在Ubuntu中,我注意到有2个文件。真正的是/etc/init.d/rc.local
; 看来对方/etc/rc.local
是假的?
修改正确的代码(/etc/init.d/rc.local
)后,它的执行效果就和预期的一样。