UpStart initctl start|restart ubuntu

14,936

Solution 1

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.

Solution 2

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

Solution 3

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.

Share:
14,936

Related videos on Youtube

Niclas
Author by

Niclas

Updated on June 04, 2022

Comments

  • Niclas
    Niclas about 2 years

    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.

  • Niclas
    Niclas over 12 years
    Thanks. I know. Strange that initctl doesn't support it. The job is identified.