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.
-
http://stackoverflow.com/questions/7080315/linux-equivalent-for-windows-startup http://stackoverflow.com/questions/4424703/run-c-program-at-startup and http://stackoverflow.com/questions/3358300/programmatically-run-at-startup-on-linux – Flexo Aug 28 '11 at 15:21
-
23@Flexo This answer is not present in neither of the links you provided. +1 to this question and answer – Paco Valdez Jul 06 '12 at 15:19
-
2In fact, the answer here is referenced from the first of those questions. – Wilson F Sep 19 '14 at 17:09
-
5For a simple, portable way to do this, you can use Cron. Run `crontab -e` to edit your user's crontab; add `@reboot command` to run `command` on each boot. – appas May 27 '15 at 17:29
-
2I dont know if its a duplicate or not but it is the first google hit.. – Mister Verleg May 12 '16 at 07:49
-
Check [this](https://stackoverflow.com/a/57197654/8460132) out. Hope it helps. – amrezzd Jul 25 '19 at 08:55
1 Answers
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 .

- 30,738
- 21
- 105
- 131

- 5,262
- 1
- 34
- 37
-
1Just 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.) – Dave Lasley Aug 28 '11 at 15:18
-
Thank you very much, I'll check it as soon as possible! Do you know if it is possible to do something like this on a mac computer as well? It should be a unix os not unlike linux... so do you know if there's any similar way to do this on mac as well? Thank you again! – Matteo Monti Aug 29 '11 at 00:19
-
Macs a lot easier. Just put the script in the Login Items list in your Accounts preferences – Dave Lasley Aug 29 '11 at 03:15
-
5I 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 '12 at 00:23
-
This does not run in an X terminal, he apparently wants to do it on starting the desktop. I gave some sort of answer here http://superuser.com/questions/528930/open-terminal-on-start-and-pass-a-command – Sam Watkins Jan 07 '13 at 05:15
-
-
1@puk Both hard links and symbolic links will work with this method – Dave Lasley Oct 01 '13 at 16:15
-
This does not work for me. I have "Ubuntu Server 13.04" (Linux ubuntu 3.8.0-30-generic #44-Ubuntu). I just tried to run my ***/etc/init.d/emacs-server*** (`emacs --daemon`) command on boot, using these instructions. Anything different I need to do? – Nutritioustim Oct 04 '13 at 14:36
-
@Nutritioustim - I am able to run `emacs --daemon` on boot using the instructions provided. What error message are you receiving? Did you remember to include the shebang (`#!/bin/sh`) at the top of your script file? – Dave Lasley Oct 08 '13 at 22:25
-
@Dave, actually, my ***./etc/init.d\/emacs-server*** has `#!/bin/bash emacs --daemon`. When I look at ***var/log/boot.log ***, I get an `/etc/rc2.d/S20emacs-server: line 3: emacs: command not found` error message. See [link](http://pastie.org/8391967). – Nutritioustim Oct 10 '13 at 10:44
-
@Nutritioustim - That means that emacs is not in root's environment variables. Run `which emacs` to find the full path of emacs. At this point, you can either add the directory into root's path or just use the full path in your `emacs-server` script (most likely `/usr/bin/emacs`). I would recommend the latter – Dave Lasley Oct 10 '13 at 19:49
-
@Dave, Did that. But I still get the error [here](http://pastie.org/8393507). I tried `/usr/local/bin/emacs --daemon --with-x-toolkit=lucid`, as per log suggestion. But no dice :/ – Nutritioustim Oct 10 '13 at 21:47
-
@Nutritioustim - Sorry, I don't have much experience in emacs so I'm not too sure how to guide you from here. This looks to be an issue on their end :/ – Dave Lasley Oct 11 '13 at 01:21
-
14Just 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: https://github.com/fhd/init-script-template – Charles Brandt Nov 18 '13 at 12:11
-
1Reading this - http://unix.stackexchange.com/questions/28679/whats-the-connection-between-etc-init-d-and-etc-rcx-d-directories-in-linu - along with the answer will be helpful – Arjun Sreedharan May 03 '15 at 07:31
-
Does "Should be an init style script" mean it's mandatory or can it be anything? – Cyberlurk Jul 20 '15 at 12:31
-
-
1This works only on default mode of update-rc.d. According to 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. – Russell Yan Oct 28 '15 at 02:49
-
@CharlesBrandt , I still need to run `/etc/init.d/algorithms start` command. But I want it would run **automatically** in startup. Is there a way? – partho Jan 22 '16 at 16:34
-
-
worked like a charm for one daemon. however when i tried to use it for 'tranmission-gtk --minimized' it didn't work. So, I tried '$ gnome-session-properties' and it worked. – user3804598 Jan 02 '18 at 20:13
-