140

I'd need a program to be run every time I start up my Ubuntu Linux. So I'd need to add it to my startup programs list. Just one problem: I'd need to do it via the terminal.

6

1 Answer 1

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

The script should now start on boot. Note that this method also works with both hard links and symbolic links (ln).

At this point in the boot process PATH isn't set yet, so it is critical that absolute paths are used throughout. But, as pointed out in the comments by Steve HHH, explicitly declaring the full file path (/etc/init.d/filename) for the update-rc.d command is not valid in most versions of Linux. Per the manpage for update-rc.d, the second parameter is a script located in /etc/init.d/*.

Also as pointed out in the comments (by Charles Brandt), /filename must be an init style script. A good template was also provided - System V init script template.

As pointed out in the comments (by Russell Yan), this works only on default mode of update-rc.d.

According to the manual of update-rc.d, it can run on two modes: "the machines using the legacy mode will have a file /etc/init.d/.legacy-bootordering", in which case you have to pass sequence and runlevel configuration through command line arguments.

The equivalent argument set for the above example is

sudo update-rc.d filename start 20 2 3 4 5 . stop 20 0 1 6 .

22
  • 1
    Just as an FYI, the option “defaults” puts a link to start the script in run levels 2, 3, 4 and 5. (and puts a link to stop in 0, 1 and 6.) Aug 28, 2011 at 15:18
  • 5
    I just tried this in Ubuntu Server 12.04 LTS, but in 12.04 you can't have the /etc/init.d prefix. It's just sudo update-rc.d filename defaults.
    – Steve HHH
    Dec 20, 2012 at 0:23
  • 1
    @puk Both hard links and symbolic links will work with this method Oct 1, 2013 at 16:15
  • 14
    Just wanted to add that /filename in this case should be an init style script. Here is a template for starting your own custom init script: github.com/fhd/init-script-template Nov 18, 2013 at 12:11
  • 1
    Reading this - unix.stackexchange.com/questions/28679/… - along with the answer will be helpful May 3, 2015 at 7:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.