UpStart initctl start|restart ubuntu

Niclas picture Niclas · Feb 28, 2012 · Viewed 14.5k times · Source

When using upstart on ubuntu how do I issue a command for starting a job if not running and restarting if already running. When deploying an app to a new node the job is not defined.

initctl restart JOB complains if not already running
initctl start JOB complains if already running.

I can script it to do

initctl start JOB
initctl restart JOB

But it doesn't seem to be the nicest thing to do.

Answer

hmalphettes picture hmalphettes · Mar 16, 2012

I was in front of the same problem. Short of a straight "lazy-stop-then-start" command built-in initctl, we have to script.

Invoke start and restart if it fails:

initctl start JOB || initctl restart JOB

This script is probably not the answer both of us were looking for but it is short enough to mention it.

As long as the service works nicely, it will do the trick.

When the services fails, this script fails twice; For example, if the service was stopped and actually fails to start, it will also fail to restart.

Definitely looking for an improvement to this.

I hope this helps.