Last Updated: February 25, 2016
· adnaan

Upstart scripts for a Go Web App

Wrote simple scripts to start/stop a go web app. Sometimes we need to start a pool of worker apps on specific ports. Following scripts help.

These scripts are system jobs for upstart and are to be placed in /etc/init.


description     "App-Worker"
author          "Adnaan"

start on (net-device-up
          and local-filesystems
          and runlevel [2345])

stop on runlevel [06]

instance $PORT

    #initramfs provides early userspace
    exec 2>>/dev/.initramfs/app-worker.log
    set -x
    export APP="/root/goworkspace/src/myappcode"
    #change directory or go won't read the web app resources
    chdir $APP
    exec sudo $APP/app --port $PORT
end script

The above script can be directly used as such:

Start a worker

service app-worker start PORT=8085

Stop a worker

service app-worker stop PORT=8085

But what if need a bunch of workers. Can't go starting them individually now, can we?


description "App Pool"
author       "Adnaan"

start on (local-filesystems and net-device-up and runlevel [2345])

stop on shutdown

pre-start script
    #initramfs provides early userspace
    exec 2>>/dev/.initramfs/app-pool.log
    set -x

    start app-worker PORT=8085
    start app-worker PORT=8086
    #add more jobs..
end script

Start a Pool

service app-pool start

Stop the Pool

service app-pool stop

The above won't have any effect since it restarts the workers again. We need to individually stop the workers. Let's find our workers.

initctl list | grep "app" 

Choose your worker from the list and stop it as described above. I realize that we are individually stopping the workers. Well I am still looking for a good solution to do that in a single step. Suggestions welcome!

2 Responses
Add your response

For stopping have each worker listening on an event:

stop on shutdown or workers-stop

And then, when you want to shutdown your workers you just emit that event.

initctl emit workers-stop

over 1 year ago ·

See also this post, which accomplishes something very similar:

over 1 year ago ·