使用rc.local运行脚本:脚本可以工作,但不能在启动时运行

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)

  • 为什么这被否决?它正确指定了完整路径,但也使人们可以轻松地对其余脚本进行故障排除. (5认同)
  • 这里有极好的帮助!谢谢John Doe (3认同)
  • 很有用!帮我调试了我的rc.local脚本. (2认同)

小智 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只是一个符号链接.


Jur*_*man 5

我最终得到了暴发户,这很好.


小智 5

在Ubuntu中,我注意到有2个文件。真正的是/etc/init.d/rc.local; 看来对方/etc/rc.local是假的?

修改正确的代码(/etc/init.d/rc.local)后,它的执行效果就和预期的一样。

  • 略过`/ etc / init.d / rc.local`,看来`/ etc / rc.local`是由前者执行的。 (12认同)