Restarting Upstart instance processes

Ross picture Ross · Aug 23, 2012 · Viewed 13.4k times · Source

I am running multiple instances of a worker as described in this answer: Starting multiple upstart instances automatically

Question: Can I restart all instances at once?

To start my workers I can do:

initctl start my-workers

Which then allows me to do:

initctl status worker N=1 worker (1) start/running, process 551

initctl status worker N=2 worker (2) start/running, process 552

Is there a way to do something like this:

initctl restart my-workers

I would like to be able to restart all instances without having to know how many are running.

Here is my my-workers.conf

start on stopped cloud-init
stop on shutdown

env NUM_WORKERS=4

script
  for i in `seq 1 $NUM_WORKERS`
    do
      start worker N=$i
    done
end script

And worker.conf

stop on shutdown

chdir /path/to/current

respawn

instance $N

script
  exec su -c "/home/worker/.rvm/bin/rvm-shell -c 'bundle exec rake work 2>&1 >> /var/log/worker-$N.log'" worker
end script

Answer

SpamapS picture SpamapS · Sep 7, 2012

In worker.conf you just need to change this line:

stop on shutdown

To:

stop on stopping my-workers

And change my-workers.conf to use pre-start instead of script:

pre-start script
  for i in `seq 1 $NUM_WORKERS`
  do
    start worker N=$i
  done
end script

Now my-workers will keep state: since the work happens in pre-start, the my-workers main process won't exist and so won't exit. stop on stopping my-workers causes the workers to stop whenever my-workers is stopped. Then of course when it starts up again it will start the workers again.

(FYI, stop on shutdown does nothing, as shutdown is not a system event. man upstart-events for all the defined events) so you should also change my-workers to stop on runlevel [06]