101

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?

JW0914
  • 107
pedroo
  • 1,215
  • 1
    Where is the xsetwacom command? /etc/rc.local is executed with a very simple PATH at startup. It probably differs from the PATH you have logged in at the terminal. In the terminal, type: type -p xsetwacom then replace the "xsetwacom" command in /etc/rc.local with the full path as returned by the "type -p". Do you have to do any manual initializations before xsetwacom works for you? – waltinator Sep 28 '11 at 22:09
  • 6
    Using systemd as startup manager make sure the rc.local compatibility is running: systemctl status rc-local.service – rubo77 Apr 21 '16 at 08:09
  • waltinator's comment was my problem. Because of the minimal path, it didn't know how to run the scripts I wanted to be run. The path loaded on my Ubuntu 14.04 system at the time of runninig /etc/rc.local was the following: /sbin:/usr/sbin:/bin:/usr/bin... you could check yours by putting echo $PATH > /home/rc_local_path into your /etc/rc.local and then checking the file after it's been run on startup. – RaisinBranCrunch Dec 31 '16 at 06:44
  • Keep in mind that if your script needs to run continuously you have to start it as a daemon in the rc.local file! – totymedli Jan 30 '17 at 14:02
  • Use sudo systemctl enable rc-local.service to ensure that /etc/rc.localis executed during the server startup – William Nov 17 '17 at 10:36

4 Answers4

77

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
    
JW0914
  • 107
  • 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! – pedroo Oct 28 '10 at 08: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. –  Oct 28 '10 at 08:50
  • 4
    I've tried the "ls -l /etc/rc.local" and it is executable, but I cannot make it run on startup... Any idea? – pedroo Oct 28 '10 at 23:49
  • @pedroo: What does this script do? Does it write to any log files that you need to check? (I think it's time to update the question with more details.) –  Oct 28 '10 at 23:51
  • It's a xsetwacom command. When I run it in terminal, it works. But it doesn't run automatically when booting... – pedroo Oct 29 '10 at 00:02
  • 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. –  Oct 29 '10 at 00:07
  • The commands in .xinitrc didn't work too. But I've managed to get it to work by putting the 'rc.local' on StartUp Applications. Problem Solved! Thanks for your input! – pedroo Oct 30 '10 at 12:39
  • @pedroo: Do not put rc.local on Startup Applications. Instead put the xsetwacom command you want to run there, or create a new script with multiple commands and call it from Startup Applications. –  Oct 30 '10 at 12:45
  • I'm kind of a newbie to linux. How do I create a new script with multiple commands? Why can't I put rc.local on Startup Applications? Thanks. – pedroo Oct 30 '10 at 15:00
  • 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. –  Oct 30 '10 at 15:10
  • It works. Nice! – pedroo Oct 30 '10 at 17:28
62

In my case, none of the instructions were a perfect solution, so try this detailed one:

  1. Save all executing code in a separate text file with an arbitrary name, such as foo.sh
  2. Add #!/bin/sh as the first line in foo.sh, executing it via sudo foo.sh to check for errors
  3. In /etc/rc.local, place the full pathname to foo.sh, prefaced with sh, before exit 0:
    sh '/path/to/your/script/foo.sh'
    
  4. Verify the first line in /etc/rc.local is #!/bin/sh -e
  5. Ensure /etc/rc.local is executable:
    sudo chown root /etc/rc.local
    sudo chmod 755 /etc/rc.local
    
  6. Verify everything works fine:
    sudo /etc/init.d/rc.local start
    
  7. Reboot to test
JW0914
  • 107
shvahabi
  • 952
  • 7
    Step #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. – Peter Gluck Jul 19 '15 at 02:22
  • Prefect and step to step greats, it's works on centos but sudo /etc/rc.local start first run. – A1Gard Oct 28 '15 at 11:35
  • Thanks - this break down really helped for me and as above step #8 was the key for me too! Without that it woudn't redirect +1 (for reference, node app running on bitnami cloud server using forever for continuous running). – fidev Sep 29 '16 at 08:56
  • 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 – SeanClt Jun 17 '17 at 19:19
  • 2
    In 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? – Daniel Alder Feb 12 '20 at 00:37
43

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

screenshot

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).

rubo77
  • 32,486
4

2 suggestions.

  1. Ensure that the target script file is also marked executable.
  2. 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 :)

Ankit
  • 71