Managing Long Running Processes With Supervisor

Supervisor is a great tool, written in python, to control processes. Actually it’s a client/server system that allows sysadmins to monitor and control processes on UNIX-like servers. If your setup is in python already, then you’d better have a look cause it’s gonna be handy a lot.

Installation

Install supervisor via pip.

[sudo] pip install supervisor

This also creates by default in /etc/supervisor/conf.d/ a configuration file.

Usage

In supervisor config file, you can easily define programs, which actually represent your long running processes. For example, a simple config file would look like this:

[program: my_script]
command=/path/to/script.py
directory=/path/to/working/directory
stdout_logfile=/path/to/logs/out.log
stderr_logfile=/path/to/logs/err.log
autostart=true
autorestart=true

The configuration is pretty simple and you can easily understand what it does. If you run your script.py manually, the process would occupy one tab of your terminal so that it had somewhere to output the results. Instead of running it with nohup, you can run in under supervisor which will also monitor it’s status and ensures it will be up and running as long as you want it to.

Supervisorctl

Supervisorctl is the command line tool we use in order to manage supervisor processes. With supervisorctl we also update supervisor’s configuration, with the following:

supervisorctl reread
supervisorctl update

There is a range of commands which you can run using it, like restart [program_name], to restart the selected program defined into your config file,  status to check the status of your supervised processes, start [program_name] to start a specific program and tail -f [program_name] to open a stream with the latest logs of the selected supervised program. When you are done, exit supervisorctl and ensure that everything is running as expected.

Web Interface

Add the following to your supervisor config file, in order to see the neat web interface that supervisor provides:

[inet_http_server]
port=*:9001

It offers process monitoring and management functionalities, which some of you might find interesting and time saving. It’s worth trying.