linux - 使用 rc.local : script works, 运行脚本,但不在启动时

标签 linux bash ubuntu boot autorun

我有一个 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/

相关文章:

php - 如何在 Windows 2003 上使用 PHP 将 ODBC 连接到 DB2?

linux - 如何使用 C 或 shell 在 Linux 中检查密码?

python - 从shell脚本向python程序插入参数

linux - rake -T ... noglob : command not found

linux - 嵌入式设备为何以及何时会同时具有 NAND 和 NOR?

linux - snmpbulk 获取数据的时间太长

linux - 在启动时触发 bash 脚本以定期执行

java - 用于使用参数调用 Java 程序的 Linux shell 脚本

linux - 终端:在窗口中打开当前路径?

php shell命令错误GLIBCXX_3.4.9未找到