UpStart initctl start|restart ubuntu
Asked Answered
M

3

14

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.

Myelitis answered 28/2, 2012 at 9:47 Comment(0)
M
10

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.

Moonset answered 16/3, 2012 at 7:0 Comment(1)
Thanks. I know. Strange that initctl doesn't support it. The job is identified.Myelitis
H
7

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.'

Hardaway answered 1/5, 2014 at 21:15 Comment(0)
V
2
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.

Vaisya answered 5/5, 2014 at 21:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.