Upstart, one job definition to start and one to stop

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 ??


Solution 1:

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.

Solution 2:

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.