HoganHost blog

[email protected]

A Complete Guide to Supervisor in Linux with Examples

 

 

 

 

 

 

 

 

 

 

A Complete Guide to install Supervisor on Alma Linux server  Examples

Supervisor is a process manager open source tool which monitors and control the lightweight, long running processes. More on Official website. In this tutorial we will create a simple application and see how to manage supervisor configurations for multiple applications.

supervisord -> server piece of supervisor is named as supervisord

supervisorctl -> client piece of supervisor is named as supervisorctl

We will perform below steps to have a complete understanding about supervisor in Linux.

  • Install supervisor
  • Running Supervisor
  • Create Program
  • Create configuration File

 

A Complete Guide to Supervisor in Linux with Examples

Step 1: Prerequisites

a) You should have a running Linux (in our case it is CentOS 7 and Almalinux 8) Server.

b) You should have sudo or root access to run privileged commands.

c) You should have atleast one package manager(yum or dnf in this case) available in your Server.

 

Step 2: Update Your Server

Before installing any new package, you need to first update the package cache with yum update or dnf update command as shown below. If you are using Debian/Ubuntu based systems, then use apt update or apt-get update command.

[root@cyberithub ~]# yum update

 

Step 3: Enable EPEL

For Centos, supervisor is present in EPEL repository. Hence first install epel-release package and then install supervisor. To enable EPEL repo, you need to use yum install epel-release command as shown below.

[root@cyberithub ~]# yum install epel-release
Last metadata expiration check: 0:14:33 ago on Wed 02 Feb 2022 08:06:02 PM IST.
Dependencies resolved.
==================================================================================================================================
 Package                           Architecture                Version                          Repository                   Size
==================================================================================================================================
Installing:
 epel-release                      noarch                      8-11.el8                         extras                       24 k

Transaction Summary
==================================================================================================================================
Install  1 Package

Total download size: 24 k
Installed size: 35 k
Is this ok [y/N]: y

Downloading Packages:
epel-release-8-11.el8.noarch.rpm                                                                  8.8 kB/s |  24 kB     00:02
----------------------------------------------------------------------------------------------------------------------------------
Total                                                                                             8.8 kB/s |  24 kB     00:02
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                          1/1
  Installing       : epel-release-8-11.el8.noarch                                                                             1/1
  Running scriptlet: epel-release-8-11.el8.noarch                                                                             1/1
  Verifying        : epel-release-8-11.el8.noarch                                                                             1/1

Installed:
  epel-release-8-11.el8.noarch

Complete!

 

Step 4: Install Supervisor

Next step is to install supervisor by using yum install supervisor -y command as shown below. If you are using any Ubuntu/Debian based systems, then you need to use apt install supervisor -y command.

[root@cyberithub ~]# yum install supervisor -y
Extra Packages for Enterprise Linux Modular 8 - x86_64                                            182 kB/s | 979 kB     00:05
Extra Packages for Enterprise Linux 8 - x86_64                                                    3.5 MB/s |  11 MB     00:03
Last metadata expiration check: 0:00:02 ago on Wed 02 Feb 2022 08:21:19 PM IST.
Dependencies resolved.
==================================================================================================================================
 Package                        Architecture       Version                                            Repository             Size
==================================================================================================================================
Installing:
 supervisor                     noarch             4.2.2-1.el8                                        epel                  576 k
Installing dependencies:
 python3-pip                    noarch             9.0.3-19.el8                                       appstream              20 k
 python3-setuptools             noarch             39.2.0-6.el8                                       baseos                163 k
 python36                       x86_64             3.6.8-2.module_el8.4.0+790+083e3d81                appstream              19 k
Enabling module streams:
 python36                                          3.6

Transaction Summary
==================================================================================================================================
Install  4 Packages

Total download size: 777 k
Installed size: 3.4 M
Downloading Packages:

(1/4): python36-3.6.8-2.module_el8.4.0+790+083e3d81.x86_64.rpm                                     10 kB/s |  19 kB     00:01
(2/4): python3-pip-9.0.3-19.el8.noarch.rpm                                                         10 kB/s |  20 kB     00:01
(3/4): supervisor-4.2.2-1.el8.noarch.rpm                                                          1.2 MB/s | 576 kB     00:00
(4/4): python3-setuptools-39.2.0-6.el8.noarch.rpm                                                  65 kB/s | 163 kB     00:02
----------------------------------------------------------------------------------------------------------------------------------
Total                                                                                             231 kB/s | 777 kB     00:03
warning: /var/cache/dnf/epel-6519ee669354a484/packages/supervisor-4.2.2-1.el8.noarch.rpm: Header V4 RSA/SHA256 Signature, key ID 2f86d6a1: NOKEY

Extra Packages for Enterprise Linux 8 - x86_64                                                    1.6 MB/s | 1.6 kB     00:00
Importing GPG key 0x2F86D6A1:
 Userid     : "Fedora EPEL (8) <[email protected]>"
 Fingerprint: 94E2 79EB 8D8F 25B2 1810 ADF1 21EA 45AB 2F86 D6A1
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                          1/1
  Installing       : python3-setuptools-39.2.0-6.el8.noarch                                                                   1/4
  Installing       : python36-3.6.8-2.module_el8.4.0+790+083e3d81.x86_64                                                      2/4
  Running scriptlet: python36-3.6.8-2.module_el8.4.0+790+083e3d81.x86_64                                                      2/4
  Installing       : python3-pip-9.0.3-19.el8.noarch                                                                          3/4
  Installing       : supervisor-4.2.2-1.el8.noarch                                                                            4/4
  Running scriptlet: supervisor-4.2.2-1.el8.noarch                                                                            4/4
  Verifying        : python3-pip-9.0.3-19.el8.noarch                                                                          1/4
  Verifying        : python36-3.6.8-2.module_el8.4.0+790+083e3d81.x86_64                                                      2/4
  Verifying        : python3-setuptools-39.2.0-6.el8.noarch                                                                   3/4
  Verifying        : supervisor-4.2.2-1.el8.noarch                                                                            4/4


Installed:
python3-pip-9.0.3-19.el8.noarch   python3-setuptools-39.2.0-6.el8.noarch   python36-3.6.8-2.module_el8.4.0+790+083e3d81.x86_64
supervisor-4.2.2-1.el8.noarch

Complete!

As soon as installation completes, you will see a directory “supervisord.d” and a file “supervisord.conf” inside /etc as shown below.

[root@cyberithub ~]# cd /etc
[root@cyberithub etc]# ls supervisor*
supervisord.conf
supervisord.d:

 

Step 5: Running Supervisord daemon

Next step is to start and enable the supervisord daemon using below command.

[root@cyberithub ~]# systemctl start supervisord
[root@cyberithub ~]# systemctl enable supervisord
Created symlink /etc/systemd/system/multi-user.target.wants/supervisord.service → /usr/lib/systemd/system/supervisord.service.

Check the status of supervisord daemon using systemctl status supervisord command, it should be in running state by now.

[root@cyberithub ~]# systemctl status supervisord
supervisord.service - Process Monitoring and Control Daemon
Loaded: loaded (/usr/lib/systemd/system/supervisord.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2022-02-02 20:31:38 IST; 11s ago
Main PID: 57905 (supervisord)
Tasks: 1 (limit: 23679)
Memory: 16.2M
CGroup: /system.slice/supervisord.service
               └─57905 /usr/bin/python3.6 /usr/bin/supervisord -c /etc/supervisord.conf

Feb 02 20:31:38 cyberithub.example systemd[1]: Starting Process Monitoring and Control Daemon...
Feb 02 20:31:38 cyberithub.example systemd[1]: Started Process Monitoring and Control Daemon.

Next we have to generate the supervisord configuration file. Supervisord can generate its own config file using the command “echo_supervisord_conf”. Let’s redirect the output of this command to a file in the path where we want the configuration file to be placed.

[root@cyberithub ~]# echo_supervisord_conf > /etc/supervisord.conf

NOTE:

Please note above command generates a template of supervisord.conf file which we can modify based on our need and requirement.

 

Step 6: Create a Sample Script

For our tutorial, we will create a simple script called sample.sh under /home/cyberithub directory. You are free to choose the script name and path. In the below script, we will direct the current hostname to the standard output and "redirect error" to standard error. Then it will sleep for one second and start the loop all over again. Since the condition is always set to true so it will keep on running the same steps and hence it will run indefinitely.

[root@cyberithub ~]# vi /home/cyberithub/sample.sh
#!/bin/bash
while true
do
     # Echo hostname to stdout
      echo `hostname`
     # Echo 'redirect error' to stderr
      echo 'redirect error' >&2
      sleep 1
done

Save the file by pressing Ctrl+C and then enter :wq! to save and exit. If you are using Ubuntu/Debian based system then I would recommend you to use nano editor instead of vi as it is quite easy to use. Then save and exit the file by pressing Ctrl+X.

Once the sample file is created, you need to provide execute permission to run the script. This can be done by using chmod +x /home/cyberithub/sample.sh command as shown below.

[root@cyberithub ~]# chmod +x /home/cyberithub/sample.sh

 

Step 7: Configuration File

Note that for each supervisor program, there must be a respective .conf file in the path /etc/supervisord.d/. We will create a config file for this program called sample.conf.

[root@cyberithub ~]# cd /etc/supervisord.d/
[root@cyberithub supervisord.d]# vi sample.conf
command=/home/cyberithub/sample.sh
autostart=true
autorestart=true
stderr_logfile=/tmp/test.err
stdout_logfile=/tmp/test.out

Last step would be to modify the supervisord.conf file to update the path from where the .conf file should be pick up. Rest all can be left default or changed totally based on the requirement. Here we are modifying the include block as shown below.

[include]
files =/etc/supervisord.d/*.conf

Then we will execute below two commands to make the changes effective.

[root@cyberithub ~]# supervisorctl reread
sample: available
[root@cyberithub ~]# supervisorctl update
sshd: updated process group

Once changes are read, there should be application logs generated on the path which we have configured in the configuration file. In our case it is inside /tmp folder.

[root@cyberithub ~]# ll /tmp/
-rw-r--r-- 1 root root     1605 Feb  2 17:26 test.err
-rw-r--r-- 1 root root     2675 Feb  2 17:26 test.out

As defined in the script logic, test.out will capture the hostname as you can see below.

[root@cyberithub ~]# cat /tmp/test.out
cyberithub.example
cyberithub.example
cyberithub.example
cyberithub.example
cyberithub.example
cyberithub.example

As defined in the script logic, test.err will capture the string “redirect error” as you can see below.

[root@cyberithub ~]# cat /tmp/test.err
redirect error
redirect error
redirect error
redirect error
redirect error
redirect error
redirect error
redirect error
redirect error
redirect error
redirect error

 

Step 8: Check More Options

You can check all the actions available with supervisorctl using supervisorctl help command as shown below.

[root@cyberithub ~]# supervisorctl help
default commands (type help <topic>):
=====================================
add    exit      open  reload  restart   start   tail
avail  fg        pid   remove  shutdown  status  update
clear  maintail  quit  reread  signal    stop    version
nnnCategorie

 

Leave a Comment

Your email address will not be published. Required fields are marked *