I have a question, how can I run my own bash script at every startup time in Ubuntu. Suppose I have a script which is doing a particular kind of work. Now I want it to run automatically at the time of starting my Ubuntu system.
4 Answers
You should learn how to use upstart. See this.

- 30,364
- 7
- 62
- 85
-
2or if it's too difficult for you, you can add a reference to your script in `/etc/rc.local` (put it before the `exit 0` line!) – Carlos Campderrós Mar 26 '12 at 13:11
Currently Linux systems (including Ubuntu) support 2 ways of achieving this: Upstart and SysV scripts. Upstart is the "new" way.
Generating SysV scripts can be achieved like so:
update-rc.d <your script> defaults
This will make links to start the service in runlevels 2345 and to stop the service in runlevels 016 and will create the appropriate SysV-style scripts inside /etc/rc?.d/
The other way would be to write an upstart-job. Upstart jobs are located under /etc/init. The easiest way is to copy an existing job and try to modify it for your script. Here's the upstart stanzas explained.

- 30,580
- 6
- 55
- 83
I have recently run into a situation where one job is ideally started with upstart, and the other one rc.local. Although both methods will get your script executed, upstart makes more sense when daemonizing a script at start time; rc.local, on the other hand, lets the script run its course.
For example, if the script is already a daemon process, e.g. a server, it can be invoked by rc.local, as it doesn't stay in the foreground and block the terminal.
But if the script itself is not daemonized, upstart will give it the ability to gracefully start and stop, upon system events. This is good for running a non-exiting PHP script in the background. Although you could use upstart to run, say, Apache httpd, the process "exits" right away in the eyes of Upstart, which makes Upstart pointless, as it is already "terminated".

- 3,855
- 1
- 16
- 29
There are two options. The easy one, edit /etc/rc.local and call your script from there. The other option is to use upstart. Hace a look at /etc/init/hostname.conf. You can use that file as template, copy it as /etc/init/yourscript.conf, adapt the content and it should work.

- 32,892
- 1
- 18
- 20