Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems. It is used to control processes related to a project or a customer and is meant to start like any other program at boot time. Features of Supervisor includes simple, centralized, efficient, extensible, compatible and proven.
The components of Supervisor are:
supervisord: This is the server piece of supervisor is named supervisord. It is responsible for starting child programs at its own invocation, responding to commands from clients, restarting crashed or exited subprocesseses, logging its subprocess stdout and stderr output, and generating and handling “events” corresponding to points in subprocess lifetimes.
supervisorctl: The command-line client piece of the supervisor is named supervisorctl. It provides a shell-like interface to the features provided by supervisord.
Web Server: A (sparse) web user interface with functionality comparable to supervisorctl
XML-RPC Interface: The same HTTP server which serves the web UI serves up an XML-RPC interface that can be used to interrogate and control supervisor and the programs it runs.
In this guide, we will install the latest version of supervisor, show how to spawn and manage programs through supervisorctl, and lastly configure a web interface to manage our programs.
Supervisor is known to work with Python 2.4 or later but will not work under any version of Python 3.
To check what version of Python is installed on your system, type the command below:
python --version
Installing Supervisor
Supervisor can be installed through two ways:
By downloading the Supervisor package and invoking the command:
apt-get install supervisor
By using
easy_install
which is a feature of python setuptools. Firstly, we need to update our local packages list by running the command:apt-get update
Now, we install easy install
with the command below followed by y when prompted to continue:
apt-get install python-setuptools
We can now install supervisor by typing the command:
sudo easy_install supervisor
After installation, the supervisor daemon should be started, as the prebuilt packages come with an init script that will also ensure the Supervisor is restarted after a system reboot.
Basic Configuration:
Once the installation is complete, we have to generate our configuration file. Create a folder named supervisor inside /etc.
sudo mkdir /etc/supervisor
After the installation of Supervisor has completed, run the command echo_supervisord_conf
to print a “sample” Supervisor configuration file to your terminal stdout.
echo_supervisord_conf
Next, type the command below as root:
echo_supervisord_conf > /etc/supervisor/supervisord.conf
To add programs to be managed by supervisor we just need to create the appropriate [program] sections in /etc/supervisor/supervisord.conf file. We will be using the [include] section in order to avoid tampering with system settings. Locate the section below, uncomment it and then edit it to look like the following.
[include]
files=conf.d/*.conf
For each program we would like to add to supervisor, we will be creating a .ini file inside the /etc/supervisor/conf.d/
directory. Create the folder with the command below:
sudo mkdir /etc/supervisor/conf.d
Starting the supervisor server:
In order to manage and control programs, we need to start the supervisor server. We will do this by registering the supervisor server in systemd, in order for the server to be started at system startup.
Firstly, create a file called supervisord.service
in the /etc/systemd/system
directory.
touch /etc/systemd/system/supervisord.service
Open the newly file with nano and add the following below to the file:
[Unit]
Description=Supervisor daemon
Documentation=http://supervisord.org
After=network.target
[Service]
ExecStart=/usr/local/bin/supervisord -n -c
/etc/supervisor/supervisord.conf
ExecStop=/usr/local/bin/supervisorctl $OPTIONS
shutdown
ExecReload=/usr/local/bin/supervisorctl $OPTIONS
reload
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
Alias=supervisord.service
Save the file then proceed to start the supervisord service with the command below:
systemctl start supervisord.service
Check the status of the service with the command below:
systemctl status supervisord.service
Adding a Program:
For this guide, we are going to create a shell script that we want to keep running that we have saved at
supervisor_cw_hello.sh
.
sudo touch supervisor_cw_hello.sh
sudo nano supervisor_cw_hello.sh
#!/bin/bash
while true
do
echo "This is CLoudwafer"
# Echo current timestamp to stdout
echo Hello Supervisor: `date`
# Echo 'error!' to stderr
echo An error occurred at `date`! >&2
sleep 1
done
Make the script executable with the command below:
sudo chmod +x supervisor_cw_hello.sh
Next, we need to create the corresponding configuration file by running the following:
sudo touch /etc/supervisor/conf.d/supervisor_cw_hello.conf
Add the following with nano:
[program:supervisor_cw_hello]
command=/home/ayo/supervisor_cw_hello.sh
autostart=true
autorestart=true
stderr_logfile=/var/log/hello.err.log
stdout_logfile=/var/log/hello.out.log
After successfully adding a new program, we should run the following two commands, to inform the server to reread the configuration files and to apply any changes.
sudo supervisorctl reread
sudo supervisorctl update
After our configuration file has been created and saved, we can now tell Supervisor about our new program through the supervisorctl command.
sudo supervisorctl
You can see the process called supervisor_cw_hello with a RUNNING status.
To view all available commands, type help
For example, using the tail command:
The Web Server Client
To allow access to the supervisord web server, open the supervisord configuration file and locate the [inet_http_server]
section.
sudo nano /etc/supervisor/supervisord.conf
Edit it with the with the following configuration:
[inet_http_server]
port=*:9001
username=user
password=123
You can replace the username: user and password:123 with your desired credentials, save the file and the restart supervisord service.
sudo systemctl restart supervisord.service
Note that you have to allow TCP access to the port 9001 on your firewall before you can access http://{server-ip}:9001
from your browser.
For cloudwafer servers, edit the /etc/csf/csf.conf
and add the port 9001 to the list of allowed ports under TCP then proceed to restart CSF with the command sudo csf -r
At this stage, we have successfully installed and configured the latest version of supervisord.
To learn about advanced configuration, kindly refer to the official supervisord documentation.
Cloudwafer provides simple, fast and yet reliable high-performance Cloud, Custom Dedicated, and Enterprise Infrastructure hosting services. Whatever your business needs, we've got you covered! Check out our mouthwatering Cloud server offers here.