102

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,225

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
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
  • 982
46

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
  • 34,024
  • 52
  • 172
  • 299
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