Hi i just discoverd ubuntu upstart (etc/init/* scripts). And i would like to use it to run a particular application. This app is run via a command line, but there is an argument to start an instance of the app and another argument to stop it (let say 'start' and 'stop') Is there a way to use upstart for that ??

  • 1
    I don't really understand what is the problem... Can you be more specific? In particular... why can't you specify an argument: myapplicattion --start when you want this triggered, and --stop in the event(s) you want it stopped. Also, you can run a script instead of directly call the application. In that script you can do whatever you need: check if it's running, start it, stop it...
    – luri
    Jan 24, 2011 at 22:29
  • I dont know how to express ./script start (for start event) and ./script stop (for stop event). Or if it's easier i could run ./scriptStart and ./scriptStop. But i want both on the same job definition. If this may help, the script i want to run create a deamon. So alfter running the script, the shell give me back the controle (sorry for my english)
    – benzen
    Jan 24, 2011 at 22:34

3 Answers 3


It should be as simple as creating a file /etc/init/myjob.conf:

description    "My Job"

# start conditions for automatic start
# stop conditions for automatic stop

pre-start exec /path/to/program start
post-stop exec /path/to/program stop

You should then be able to do sudo start myjob to start and sudo stop myjob to stop.

See http://upstart.ubuntu.com for some more info.

  • ok but i read this 'Additional shell code can be given to be run before or after the binary or script specified with exec or script. These are not expected to start the process, in fact, they can't. They are intended for preparing the environment and cleaning up afterwards.' here upstart.ubuntu.com/getting-started.html
    – benzen
    Jan 24, 2011 at 22:40
  • True that. On the other hand, /etc/init/ufw.conf seems to do exactly what's strongly discouraged there. :-/
    – htorque
    Jan 24, 2011 at 22:44
  • ok, you are totally right. I'll give it a try.
    – benzen
    Jan 24, 2011 at 22:46
  • Given the info from your link I'm totally wrong. :D I just tested this with a small script and it seems to work, but if that's not the recommended way, then my answer is crap.
    – htorque
    Jan 24, 2011 at 22:48
  • It works, but it slows down boot up. The idea is that upstart reads all of its init/*.conf files in at one time, and then there's no more random scripts all over the filesystem to read/execute.
    – SpamapS
    Jan 27, 2011 at 13:31

If all you want to do is start/stop the service at boot time.. then drop your script in /etc/init.d and run

update-rc.d yourscript defaults

Or if that complains

update-rc.d yourscript start 20 2
update-rc.d yourscript stop 20 0
update-rc.d yourscript stop 20 1
update-rc.d yourscript stop 20 6

Both of which will add it to the bootup/shutdown.

Note that this means it will run as root.. so you may want to make sure the script switches users to your user or another one that you want this program running as.

Upstart is meant to replace such scripts if at all possible. The motivations for this are faster boot and a more clear encoding of the information needed to know when to start/stop a service. It achieves the faster boot specifically by only having to read the contents of /etc/init to get started, which is especially beneficial when you add in ureadahead to the boot process, as the less random tiny script files ureadahead has to cache, the more it can spend time on important things like libraries and programs needed to start the system up.

I'd bet that the script you're running does a few things to setup the environment, then runs the program in a daemonized mode. Your upstart job should seek to replace the script's logic, not just run the script.

  • I tryied before using upsatart to use script in /etc/init.d But the script in there are much more complexe than these in /etc/init/ And about the script i use in my upstart script, i do not writ this one, in come with my app server(the app i want to run at start up).
    – benzen
    Jan 28, 2011 at 12:26

I guess you'll want it to start or stop on different events, so it'll be easier to set up two different jobs.

If the same events toggles your app, then you could set up a job with a script that checks wether it's running or not, and hence starts or stops your app.

  • ok, seems good to me. How could i check this status inside of a job definition, Is there a nomalised way?
    – benzen
    Jan 24, 2011 at 22:44
  • initctl list will list all jobs and if they're running or stopped. So you could use script within the job instead of exec.
    – luri
    Jan 24, 2011 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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