可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a node.js script which need to start at boot and run under the www-data user. During development I always started the script with:
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>&1
to "redirect stderr to stdout" and it should be a daemon so the last character is a &
.
Finally, my /etc/rc.local
looks like this:
#!/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 node
process is running, the /var/log/php-jobs.log
does not exist and thus, the manager.js does not work. What is happening?
回答1:
In this example of a rc.local script I use io redirection at the very first line of execution to my own log file:
#!/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
回答2:
On some linux's (Centos & RH, e.g.), /etc/rc.local
is initially just a symbolic link to /etc/rc.d/rc.local
. On those systems, if the symbolic link is broken, and /etc/rc.local
is a separate file, then changes to /etc/rc.local
won't get seen at bootup -- the boot process will run the version in /etc/rc.d
. (They'll work if one runs /etc/rc.local
manually, but won't be run at bootup.)
Sounds like on dimadima's system, they are separate files, but /etc/rc.d/rc.local
calls /etc/rc.local
The symbolic link from /etc/rc.local
to the 'real' one in /etc/rc.d
can get lost if one moves rc.local
to a backup directory and copies it back or creates it from scratch, not realizing the original one in /etc
was just a symbolic link.
回答3:
I ended up with upstart, which works fine.
回答4:
In Ubuntu I noticed there are 2 files. The real one is /etc/init.d/rc.local
; it seems the other /etc/rc.local
is bogus?
Once I modified the correct one (/etc/init.d/rc.local
) it did execute just as expected.
回答5:
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 &.
回答6:
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 firstboot
service will always be in the way to rc.local
. The solution is to disable firstboot
by doing:
sudo chkconfig firstboot off
if you are not sure why your rc.local
does not run, you can always check from /etc/rc.d/rc
file because this file will always run and call other subsystems (e.g. rc.local).
回答7:
I had the same problem (on CentOS 7) and I fixed it by giving execute permissions to /etc/local:
chmod +x /etc/rc.local
回答8:
I got my script to work by editing /etc/rc.local
then issuing the following 3 commands.
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.
回答9:
I am using CentOS 7.
$ cd /etc/profile.d
$ vim yourstuffs.sh
Type the following into the yourstuffs.sh script.
type whatever you want here to execute
export LD_LIBRARY_PATH=/usr/local/cuda-7.0/lib64:$LD_LIBRARY_PATH
Save and reboot the OS.
回答10:
This is most probably caused by a missing or incomplete PATH environment variable.
If you provide full absolute paths to your executables (su and node) it will work.
回答11:
It is my understanding that if you place your script in a certain RUN Level, you should use ln -s to link the script to the level you want it to work in.
回答12:
I have used rc.local in the past. But I have learned from my experience that the most reliable way to run your script at the system boot time is is to use @reboot command in crontab. For example:
@reboot path_to_the_start_up_script.sh
回答13:
first make the script executable using
sudo chmod 755 /path/of/the/file.sh
now add the script in the rc.local
sh /path/of/the/file.sh
before exit 0
in the rc.local,
next make the rc.local to executable with
sudo chmod 755 /etc/rc.local
next to initialize the rc.local use
sudo /etc/init.d/rc.local start
this will initiate the rc.local
now reboot the system.
Done..
回答14:
1 Do not recommend using root to run the apps such as node app.
Well you can do it but may catch more exceptions.
2 The rc.local normally runs as root user.
So if the your script should runs as another user such as www U should make sure the PATH and other environment is ok.
3 I find a easy way to run a service as a user:
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...
回答15:
rc.local
only runs on startup. If you reboot and want the script to execute, it needs to go into the rc.0
file starting with the K99 prefix.