search by tags

for the user

adventures into the land of the command line

masters and workers with upstart

found it here and here:

you need two configs, one for the master process (which will not get a pid, but will run as long as the children are up), and one for the workers or instances.

the master:

$ vim /etc/init/myapp.conf

description "myapp"

start on startup
start on runlevel [2345]
stop on runlevel [016]

pre-start script
    for INSTANCE in 1 2 3 4 5
    do
        start myapp-instance INSTANCE=$INSTANCE || :
    done
end script

post-stop script
    for INSTANCE in 1 2 3 4 5
    do
        stop myapp-instance INSTANCE=$INSTANCE || :
        rm -f /var/run/myapp-instance-$INSTANCE.pid
    done
end script

the worker:

$ vim /etc/init/myapp-instance.conf

description "myapp-instance"

env USER=deploy

stop on runlevel [016]

instance $INSTANCE

respawn
respawn limit 10 10

exec start-stop-daemon --start \
  --make-pidfile --pidfile /var/run/myapp-instance-$INSTANCE.pid \
  --chuid $USER \
  --chdir /some/app/dir \
  --exec python some_app_start_command.py >> /var/log/myapp/myapp-instance-$INSTANCE.log 2>&1

start the workers by starting the master:

$ service myapp start

you can inspect with initctl or ps:

$ initctl list | grep "^myapp"
myapp-instance (5) start/running, process 113500
myapp-instance (4) start/running, process 113497
myapp-instance (3) start/running, process 113494
myapp-instance (2) start/running, process 113491
myapp-instance (1) start/running, process 113487
myapp start/running

stopping or restarting the workers:

$ service myapp restart
$ service myapp stop