Niclas
Niclas

Reputation: 1406

UpStart initctl start|restart ubuntu

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.

Upvotes: 14

Views: 15722

Answers (3)

SpamapS
SpamapS

Reputation: 1115

sudo service JOB restart

The service command was patched in Ubuntu to make it work the same on Upstart as it does in the most common cases on sysvinit.

systemctl restart JOB

Has some unexpected effects, and in general should be carefully studied before using. It is mostly there so you can restart a job without re-loading the job definition, which is a really uncommon case.

Upvotes: 2

Lasse Skindstad Ebert
Lasse Skindstad Ebert

Reputation: 3390

I also tried the 'start or restart' method that hmalphettes suggested, but got into troubles. When using this approach then updates to the upstart script would not be applied. Instead I use this, which works as I would expect:

sudo stop JOB || true && sudo start JOB

This basically reads 'Stop the job if it's running, then start it.'

Upvotes: 7

hmalphettes
hmalphettes

Reputation: 181

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.

Upvotes: 10

Related Questions