Golang upstart script for Ubuntu

Golang rocks as a language, it complies your code to a single binary which can then easily be deployed to remote systems. The problem with it compared to .NET, Java or languages like PHP or Ruby is that it is not invoked by fast CGI or any another technologies. It instead needs to be run as a background process listening to a specific port.

My choice of OS is Ubuntu deployed from code ship which means the best way to start the app and keep is running is upstart. This is a simple configuration file deployed into /etc/init and then the OS looks after the rest.

An example of my production usage is the upstart script for sleepy.fish where the binary is deployed to /opt/wwwroot/sleepy.fish/public/sleepyfish and the configuration file is /etc/init/sleepyfish.conf

description     "Sleepy Fish Upstart Script"
author          "Ollie Castle"

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

env PLATFORM=production


	chdir /opt/wwwroot/sleepy.fish/public
	exec /opt/wwwroot/sleepy.fish/public/sleepyfish

end script

You can then start your application with the following commands

sudo service sleepyfish stop
sudo service sleepyfish start
sudo service sleepyfish restart

There are plenty of tutorials about the inner workings of upstart. The most important parts to note are env of declaring environment variables to be passed to the app and chdir which sets the working directory so things like images and templates can be picked up.