I have a script I would like to run when my system starts and have put it in /etc/rc.local
, but it doesn't work. How can I enable it to run on startup?
4 Answers
Can you run your script manually; if not, it's a problem with that script, otherwise look more at rc.local
. If that script needs to run as root
, sudo
must be used to manually run it.
- Ensure
/etc/rc.local
, and the script it call, is executable:ls -l /etc/rc.local -rwxr-xr-x 1 root root 419 2010-08-27 11:26 /etc/rc.local
- Ensure
rc.local
has a shebang line, which is the default:head -n1 /etc/rc.local #!/bin/sh -e
-
Yes, I can run the script manually. How can I make sure /etc/rc.local is executable? What do I have to type? Is it "$ ls -l /etc/rc.local -rwxr-xr-x 1"? Thanks!– pedrooOct 28, 2010 at 8:47
-
@pedroo: I've copied exactly what I see in my terminal (so you see the prompt, input, and output all above). The command is "ls -l /etc/rc.local" and if it's executable, it will have those Xs in the output.– Roger PateOct 28, 2010 at 8:50
-
4I've tried the "ls -l /etc/rc.local" and it is executable, but I cannot make it run on startup... Any idea?– pedrooOct 28, 2010 at 23:49
-
1@pedroo: That needs the X server running, which doesn't happen when rc.local executes. Running it from rc.local will just make it exit without doing anything (though I hope it puts a message in syslog or elsewhere). You need to put the xsetwacom commands in ~/.xinitrc or /etc/X/xinit/xinitrc instead.– Roger PateOct 29, 2010 at 0:07
-
1@pedroo: Because rc.local contains commands intended to run at a different time. Create a new file, you can put it in ~/bin, mark it executable (chmod or properties in Nautilus), make the first line "#!/bin/bash", then put your commands on later lines.– Roger PateOct 30, 2010 at 15:10
In my case, none of the instructions were a perfect solution, so try this detailed one:
- Save all executing code in a separate text file with an arbitrary name, such as
foo.sh
- Add
#!/bin/sh
as the first line infoo.sh
, executing it viasudo foo.sh
to check for errors - In
/etc/rc.local
, place the full pathname tofoo.sh
, prefaced withsh
, beforeexit 0
:sh '/path/to/your/script/foo.sh'
- Verify the first line in
/etc/rc.local
is#!/bin/sh -e
- Ensure
/etc/rc.local
is executable:sudo chown root /etc/rc.local sudo chmod 755 /etc/rc.local
- Verify everything works fine:
sudo /etc/init.d/rc.local start
- Reboot to test
-
7Step #8 was the key, verifying that the script will run under the startup environment. In my case I need to give the full path to the command. Jul 19, 2015 at 2:22
-
Prefect and step to step greats, it's works on centos but
sudo /etc/rc.local start
first run.– A1GardOct 28, 2015 at 11:35 -
-
Step 8 was a good tip, But i think step 7 might be what fixed mine, I was trying to run the script using cron to open a tunnel but it was failing randomly– SeanCltJun 17, 2017 at 19:19
-
1In 6 and 7, you write about /etc/rc.local, but in 8, it's /etc/init.d/rc.local. On my 18.04, I have neither of these, shall I create them? Are you talking about different files or is it a mistake? Feb 12, 2020 at 0:37
On newer Ubuntu versions systemd
is used and /etc/rc.local
is not loaded always by default.
Check if the Compatibility service is loaded with
systemctl status rc-local.service
If it contains active (exited) your setting seems fine and you could have another error in your /etc/rc.local
file (this could be a command that fails for example).
-
9if rc-local.service is not activated, check this post that explains How to Enable /etc/rc.local with Systemd Feb 11, 2017 at 18:34
2 suggestions.
- Ensure that the target script file is also marked executable.
Is the target script running a sudo command? If so you might want to supply the sudo password to it.
My bad. Just check one then. Thanks for the correction enzotib :)
-
8
systemctl status rc-local.service
echo $PATH > /home/rc_local_path
into your /etc/rc.local and then checking the file after it's been run on startup.rc.local
file!sudo systemctl enable rc-local.service
to ensure that/etc/rc.local
is executed during the server startup