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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7783341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:42:48  来源:igfitidea点击:

Run script with rc.local: script works, but not at boot

linuxbashubuntubootautorun

提问by Jurian Sluiman

I have a node.js script which need to start at boot andrun under the www-data user. During development I always started the script with:

我有一个 node.js 脚本,它需要在启动时启动在 www-data 用户下运行。在开发过程中,我总是用以下方式启动脚本:

su www-data -c 'node /var/www/php-jobs/manager.js

I saw exactly what happened, the manager.js works now great. Searching SO I found I had to place this in my /etc/rc.local. Also, I learned to point the output to a log file and to append the 2>&1to "redirect stderr to stdout" and it should be a daemon so the last character is a &.

我确切地看到了发生了什么,manager.js 现在很好用。搜索所以我发现我必须把它放在我的/etc/rc.local. 此外,我学会了将输出指向日志文件并将其附加2>&1到“将 stderr 重定向到 stdout”,它应该是一个守护进程,所以最后一个字符是&.

Finally, my /etc/rc.locallooks like this:

最后,我的/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

If I run this myself (sudo /etc/rc.local): yes, it works! However, if I perform a reboot no nodeprocess is running, the /var/log/php-jobs.logdoes not exist and thus, the manager.js does not work. What is happening?

如果我自己运行它 ( sudo /etc/rc.local):是的,它有效!但是,如果我执行重新启动没有node进程正在运行,/var/log/php-jobs.log不存在,因此,manager.js 不起作用。怎么了?

采纳答案by Jurian Sluiman

I ended up with upstart, which works fine.

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

回答by w00t

You might also have made it work by specifying the full path to node. Furthermore, when you want to run a shell command as a daemon you should close stdin by adding 1<&- before the &.

您可能还通过指定节点的完整路径使其工作。此外,当您想将 shell 命令作为守护程序运行时,您应该通过在 & 之前添加 1<&- 来关闭 stdin。

回答by Boyan

In Ubuntu I noticed there are 2 files. The real one is /etc/init.d/rc.local; it seems the other /etc/rc.localis bogus?

在 Ubuntu 中,我注意到有 2 个文件。真正的一个是/etc/init.d/rc.local; 好像另一个/etc/rc.local是假的?

Once I modified the correct one (/etc/init.d/rc.local) it did execute just as expected.

一旦我修改了正确的 ( /etc/init.d/rc.local),它就会按预期执行。

回答by John Doe

In this example of a rc.local script I use io redirection at the very first line of execution to my own log file:

在这个 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

回答by CatGuyTX

I got my script to work by editing /etc/rc.localthen issuing the following 3 commands.

我通过编辑/etc/rc.local然后发出以下 3 个命令来使我的脚本工作。

sudo mv /filename /etc/init.d/
sudo chmod +x /etc/init.d/filename 
sudo update-rc.d filename defaults

Now the script works at boot.

现在脚本在启动时工作。

回答by Asad R.

This is most probably caused by a missing or incomplete PATH environment variable.

这很可能是由缺少或不完整的 PATH 环境变量引起的。

If you provide full absolute paths to your executables (su and node) it will work.

如果您提供可执行文件(su 和 node)的完整绝对路径,它将起作用。

回答by Jason

rc.localonly runs on startup. If you reboot and want the script to execute, it needs to go into the rc.0file starting with the K99 prefix.

rc.local只在启动时运行。如果您重新启动并希望脚本执行,它需要进入rc.0以 K99 前缀开头的文件。

回答by user3533658

On some linux's (Centos & RH, e.g.), /etc/rc.localis initially just a symbolic link to /etc/rc.d/rc.local. On those systems, if the symbolic link is broken, and /etc/rc.localis a separate file, then changes to /etc/rc.localwon't get seen at bootup -- the boot process will run the version in /etc/rc.d. (They'll work if one runs /etc/rc.localmanually, but won't be run at bootup.)

在某些 linux(例如 Centos 和 RH)上,/etc/rc.local最初只是指向/etc/rc.d/rc.local. 在这些系统上,如果符号链接被破坏,并且/etc/rc.local是一个单独的文件,那么/etc/rc.local在启动时不会看到对 的更改——启动过程将在/etc/rc.d. (如果/etc/rc.local手动运行它们会工作,但不会在启动时运行。)

Sounds like on dimadima's system, they are separate files, but /etc/rc.d/rc.localcalls /etc/rc.local

听起来像在 dimadima 的系统上,它们是单独的文件,但是/etc/rc.d/rc.local调用/etc/rc.local

The symbolic link from /etc/rc.localto the 'real' one in /etc/rc.dcan get lost if one moves rc.localto a backup directory and copies it back or creates it from scratch, not realizing the original one in /etcwas just a symbolic link.

如果您移动到备份目录并将其复制回来或从头开始创建它,而没有意识到原来的 in只是一个符号链接,则指向/etc/rc.local“真实” in的符号链接/etc/rc.d可能会丢失。rc.local/etc

回答by superyuan

if you are using linux on cloud, then usually you don't have chance to touch the real hardware using your hands. so you don't see the configuration interface when booting for the first time, and of course cannot configure it. As a result, the firstbootservice will always be in the way to rc.local. The solution is to disable firstbootby doing:

如果您在云上使用 linux,那么通常您没有机会用手触摸真正的硬件。所以第一次开机是看不到配置界面的,当然也不能配置。因此,该firstboot服务将始终妨碍rc.local. 解决方案是firstboot通过执行以下操作禁用:

sudo chkconfig firstboot off

if you are not sure why your rc.localdoes not run, you can always check from /etc/rc.d/rcfile because this file will always run and call other subsystems (e.g. rc.local).

如果您不确定为什么rc.local不运行,您可以随时从/etc/rc.d/rc文件中检查,因为该文件将始终运行并调用其他子系统(例如 rc.local)。

回答by fantaxy025025

1 Do not recommend using root to run the apps such as node app.

1 不推荐使用root运行node app等应用。

Well you can do it but may catch more exceptions.

好吧,您可以这样做,但可能会捕获更多异常。

2 The rc.local normally runs as root user.

2 rc.local 通常以 root 用户身份运行。

So if the your script should runs as another user such as www U should make sure the PATH and other environment is ok.

因此,如果您的脚本应以其他用户身份运行,例如 www U 应确保 PATH 和其他环境正常。

3 I find a easy way to run a service as a user:

3 我找到了一种以用户身份运行服务的简单方法:

sudo -u www -i /the/path/of/your/script

sudo -u www -i /the/path/of/your/script

Please prefer the sudo manual~ -i [command] The -i (simulate initial login) option runs the shell specified by the password database entry of the target user as a loginshell...

请首选sudo手册~ -i [命令] -i(模拟初始登录)选项运行目标用户的密码数据库条目指定的shell作为登录shell...