Monitoring a Flask Service with Supervisor
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] |
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] |
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] |
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 |
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 |
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!