Setup Supervisor using Python PIP on Ubuntu/Debian


Published: March 08, 2020 Author: Saad Ali

WARNING! Following this article, improvise if necessary. Your environment may be different than mine. I am not responsible if you screw up!

From the official site:

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

It shares some of the same goals of programs like launchd, daemontools, and runit. Unlike some of these programs, it is not meant to be run as a substitute for init as "process id 1". Instead it is meant to be used to control processes related to a project or a customer, and is meant to start like any other program at boot time.

Continuing from my previous blog post, Build and Install Python 3.7 on Ubuntu/Debian, I'll be using pip3.7 to install Supervisor instead of using apt package manager.

Install Supervisor

Install Supervisor using pip3.7:

# pip3.7 install supervisor

This will install supervisord and supervisorctl under /usr/local/bin directory.

Configure Supervisor

We need to create a configuration structure for Supervisor. We'll create the new structure in /usr/local.

# mkdir -p /usr/local/etc/supervisord/{conf-available,conf-enabled}

We'll have supervisor save all of its logs to /var/log/supervisord. Hence:

# mkdir /var/log/supervisord

Create main supervisor configuration file /usr/local/etc/supervisord/supervisord.conf with following content:

; supervisord config file

[unix_http_server]
file=/var/run/supervisord/supervisord.sock   ; (the path to the socket file)
chmod=0700                       ; socket file mode (default 0700)

[supervisord]
logfile=/var/log/supervisord/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisord            ; ('AUTO' child log dir, default $TEMP)

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisord/supervisord.sock ; use a unix:// URL  for a unix socket

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

[include]
files = /usr/local/etc/supervisord/conf-enabled/*.conf

As suggested by the above configuration, /var/run/supervisord directory will have Supervisor UNIX socket and PID files. /var/run is a symlink to /run, which is included in modern Linux distributions as a temporary filesystem, that stores volatile runtime data. This means that even if we create /var/run/supervisord manually, it will not exist once the system reboots. Systemd uses configuration files in /etc/tmpfiles.d, /run/tmpfiles.d and /usr/lib/tmpfiles.d to create and clean up volatile and temporary files.

We'll create a configuration file /etc/tmpfiles.d/supervisord.conf to make sure that /var/run/supervisord exists before Supervisor starts:

d /var/run/supervisord 0755 root root -

Next we need to create a systemd service unit /etc/systemd/system/supervisord.service which will start/restart supervisor:

[Unit]
Description=Supervisor process control system for UNIX
Documentation=http://supervisord.org
After=network.target

[Service]
ExecStart=/usr/local/bin/supervisord -n -c /usr/local/etc/supervisord/supervisord.conf
ExecStop=/usr/local/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/local/bin/supervisorctl -c /usr/local/etc/supervisord/supervisord.conf $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=50s

[Install]
WantedBy=multi-user.target

Enable and start Supervisor:

# systemctl enable supervisord && systemctl start supervisord

Note that Supervisor configuration files follow an INI configuration syntax. Hence, you can name/rename all .conf files to .ini files and it would not cause any problems. Just ensure that this change is also reflected in your systemd serivce unit.

Share

Tagged as: Python Linux Ubuntu Debian Bionic Xenial Stretch Supervisor