Motivation

My self-hosted Flask service kept crashing for no obvious reason, which was very frustrating. The top priority was to keep the service from going down, so I found the Supervisor package that can help monitor services. This post is a little tribute to Supervisor, the hero of the day.

Introduction

Supervisor is a process management tool written in Python. It provides a visual interface that makes it easy to start, restart, and stop processes. It can monitor the status of processes and automatically restart them if they crash, making it easy for developers to keep an eye on their services.

Installation Steps

The setup environment is Ubuntu 20.04.2 LTS

sudo apt-get install supervisor

There are two files to configure: the Supervisor config file and your own service config file.

Supervisor Config File

Save the sample config to your own path. I stored it under /etc/supervisor

echo_supervisord_conf > /etc/supervisor/supervisord.conf

Then open the config file to modify the parameters

vim /etc/supervisor/supervisord.conf

Modify the last two lines, pointing files to the directory where we’ll add the service config later

[include]
files = /etc/supervisor/conf.d/*.conf ;

I specifically rewrote the comments for unix_http_server, inet_http_server, and supervisorctl as follows (because I kept running into the error ProtocolError: <ProtocolError for 127.0.0.1/RPC2: 401 Unauthorized>, which was mainly an authentication issue — I found this article that solved the problem)

[unix_http_server]
file=/tmp/supervisor.sock
chmod=0700
chown=nobody:nogroup
username=user
password=123
...
[inet_http_server]
port=127.0.0.1:9001
username=user
password=123
...
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock
serverurl=http://127.0.0.1:9001
username=user
password=123

Service Config File

Add the config file for the service you want to monitor. For example, I created a demo.conf

vim /etc/supervisor/conf.d/demo.conf

In this demo.conf you can configure your service. Note the program name here — we’ll use it later!

[program:demo_server]
command = python3 manager.py runserver
directory = /home/erik/demo_server
user = erik
autostart = true
autorestart = true
stdout_logfile = /home/erik/supervisor/log/demo_server.log
stderr_logfile = /home/erik/supervisor/log/demo_server_err.log
environment = NODE_ENV='prod'

Enabling the Monitoring Service

Start Supervisor
Once everything above is configured, you can start Supervisor!

supervisord -c /etc/supervisor/supervisord.conf

Check whether the service is running on port 9001, or use ps -ef to verify

lsof -i:9001
ps -ef | grep supervisor

Starting Service Monitoring

Next, start your own service

supervisorctl start demo_server

If it fails to start, you can check what the error is

supervisorctl tail demo_server stderr

I discovered that the environment didn’t have flask_script installed (I should have used Docker to avoid environment issues).

After installing it, you can kill the service yourself and see whether Supervisor restarts it for you.

Common Supervisor Commands

supervisorctl start
supervisorctl restart
supervisorctl stop

Final Thoughts

With just a few installation steps, you can conveniently monitor your services — no more manually restarting whenever the service goes down, plus there’s a web panel to control everything. Pretty nice!