This site uses cookies to offer you a better browsing experience. Find out more on Privacy Policy.

Fake JSON service Part 2 – Supervisord

January 4, 2017, Piotr Chodziński

In the first part of this short series I described how to create a simple service which has to be running permanently.

In this second and final part, we will ensure we will be able to:

  • Run the service at system startup
  • Restart it after failure
  • Easily control it – start and stop

If we are using Unix-based system, we could use tools like SystemV, Upstart or Systemd, but all of them require some knowledge. That would cause the entry threshold to be rather high for a person who doesn’t need to configure it on a daily basis.

Supervisord

I decided to look for something easier to use and I found Supervisord.
Here is what you can read on their website:

Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.

We will use it to manage our json-server process that we created in the first part of these series.

Installation

I’m using Ubuntu as an operating system, so all I have to do is:
$ sudo apt-get install -y supervisor

Let’s check it’s been automatically launched after installation:
$ ps -A | grep supervisord

If you see something like this:

1

That means it’s already running, but if not, let’s start this manually:
$ sudo service supervisor start

Good, we have it up and running now.

Main configuration

The main configuration file is placed in /etc/supervisor and called supervisord.conf. There are some interesting options that we will adapt to our needs soon.

Permissions setup

To avoid future problems with permissions we will set them up now.
First, create new group and user:
$ sudo groupadd supervisor
$ sudo usermod -a -G supervisor

Now, logout and log-in now so the changes we’ve done take effect.

Next, edit default Supervisord config file:
$ sudo nano /etc/supervisor/supervisord.conf

Change entries in [unix_http_server] section to look like this:

21

Restart the service to make sure change are applied:
$ sudo service supervisor restart

Processes configuration

By default, each file that will be found in /etc/supervisor/conf.d folder, whose names end with .conf, will be treated as a configuration file for processes.

We know what we want – to ensure that json-server will work permanently, whether the machine is restarted and somebody forgets to run it, the server has some failure or for any other reason. We have to be sure it just works all the time.

First step is to create and open config file:
$ sudo nano /etc/supervisor/conf.d/translations-server.conf

Then put proper instructions into it:

21

All these instructions are self-descriptive, so I’m not going to describe them.

Control processes

Now we have to tell supervisor to refresh (reread command) the list and add (update command) our translations-server to managed processes. Thankfully it comes with management tool supervisorctl:
$ supervisorctl reread
$ supervisorctl update

Let’s check supervisor can see our process:
$ supervisorctl

You should see the process as a result of supervisorctl command, like this:

2

That means our service should work right now.
3

Supervisorctl command is used to manage Supervisord processes. For instance, to restart process you can do the following:
$ supervisorctl restart translations-server

And the result should be:

31

We can now kill the process, restart the machine and the translations-server will be started automatically.

Management interface

In some cases it’s not handy to manage processes with command line.
Thanks to the good will of Supervisord programmers, it’s possible to enable a web interface with some basic options.

To do this we have to open /etc/supervisor/supervisord.conf file again and add following lines at the end:

4

Then restart Supervisord:
$ sudo service supervisor restart

Done, we can manage processes using a browser!

5

That’s it!

Summary

This two-part tutorial has covered the following matters:

  • how to setup a simple JSON server;
  • how to manage a server with Supervisord.

Last posts