March 23, 2015

Process Monitoring with Upstart

We often need to monitor long running processes, from small services to our entire applications. Upstart comes with many server distributions, including Ubuntu. Learn how to use Upstart now!

We'll learn to write Upstart configuration, use initctl and write a small Golang web listener to monitor with Upstart.

We often need to monitor long running processes, from small services to our entire applications. Upstart comes with many server distributions, including Ubuntu. Learn how to use Upstart now!

We'll learn to write Upstart configuration, use initctl and write a small Golang web listener to monitor with Upstart.### What is Upstart?

An init system, created by Ubuntu. Its's popularly used as it's simpler than previous standards, such as SysVInit (System 5).

Golang

We make a web listener using Golang, because why not?

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi there!")
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8181", nil)
}

We can build this program using the following command:

go build listen

We can then start it by running:

./listen

Then head to your server's IP address, or test it using cURL:

curl localhost:8181

Upstart Conf

Let's create an Upstart configuration to keep this web listener up and running.

Create it:

sudo vim /etc/init/goweb.conf

Here's the sample Upstart for the web listener:

description "A stupid golang http listener"

start on filesystem or runlevel [2345]
stop on runlevel [!2345]

setuid www-data
setgid www-data

respawn
respawn limit 5 2

exec /opt/listen

Check it:

init-checkconf /etc/init/goweb.conf

Some initctl commands you can use with Upstart:

initctl list
initctl status goweb
initctl start goweb

We can use service as well, which will work for Upstart, falling back to SysVInit if it doesn't find an Upstart configuration for the service.

sudo service goweb status
sudo service goweb start

A More Real Example

Here's an example with more common uses within an Upstart configuration:

description "A stupid golang http listener"

start on filesystem or runlevel [2345]
stop on runlevel [!2345]

setuid www-data
setgid www-data

env API_KEY=abcdefgh

respawn
respawn limit 5 2

chdir /opt

pre-start script
    # Source File
    . /path/to/env/file
end script

script
    # Start Listener
    /opt/listen
end script

Resources

All Topics