Install And Configure Supervisor On Centos 7

In this blog post , We will see how to install and configure supervisor on Centos 7.

What Is Supervisor?

  • 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.

Components Of Supervisor

The components of supervisor are as follows:

Supervisord:

  • 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.
  • From supervisorctl, a user can connect to different supervisord processes (one at a time), get status on the subprocesses controlled by, stop and start subprocesses of, and get lists of running processes of a supervisord.

Web Server:

  • A web user interface with functionality comparable to supervisorctl may be accessed via a browser if you start supervisord against an internet socket.
  • Visit the server URL (e.g. http://localhost:9001/) to view and control process status through the web interface after activating the configuration file’s [inet_http_server] section.

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.

Minimum Requirements

  • Supervisor has been tested and is known to run on Linux (Ubuntu 9.10), Mac OS X (10.4/10.5/10.6), and Solaris (10 for Intel) and FreeBSD 6.1. It will likely work fine on most UNIX systems.
  • Supervisor is intended to work on Python 3 version 3.4 or later and on Python 2 version 2.7.
  • Supervisor will not run at all under any version of Windows.

Installing & Configuring Supervisor

Make sure you have sudo or root privileges to execute the below commands.

To install supervisor on Centos , We need to add EPEL repository to the system , Using the below command.

yum install epel-release -y

Install supervisor using the below command,

yum install supervisor -y

Once the supervisor packages are installed , We need to setup a directory for the supervisor.

sudo mkdir /etc/supervisor

We add the sample supervisor config to the supervisord.conf file.

echo_supervisord_conf > /etc/supervisor/supervisord.conf

Open the /etc/supervisor/supervisord.conf file and add the below line in the [include] section

conf.d is the folder where we will host all the programs.

 files=conf.d/*.conf
Monitor Linux Processes

Save and close the file.

To setup programs under supervisor , We need to create conf.d directory.

mkdir /etc/supervisor/conf.d

Lets start the supervisor service.

systemctl start supervisord

Check the status of the service and enable it to start on system boot,

systemctl status supervisord
systemctl enable supervisord

Adding A Program

For this tutorial , We are going to create the shell script which we will configure as a program as we want it to be keep on running.

The below setup is an example of how programs are added to supervisor and managed by supervisor.You can add your own programs.

The script we are going to add is to monitor the CPU and memory usage of the Linux process.

We will be creating 4 files under /opt directory.

Create a file named cpu-top and add the below contents.

!/bin/bash
z=$(ps aux)
while read -r z
do
var=$var$(awk '{print "cpu_usage{process=\""$11"\", pid=\""$2"\"}", $3z}');
done <<< "$z"
curl -X POST -H "Content-Type: text/plain" --data "$var
" http://localhost:9091/metrics/job/top/instance/machine

Create a file named memory-top and add the below contents.

!/bin/bash
z=$(ps aux)
while read -r z
do
var=$var$(awk '{print "memory_usage{process=\""$11"\", pid=\""$2"\"}", $4z}');
done <<< "$z"
curl -X POST -H "Content-Type: text/plain" --data "$var
" http://localhost:9091/metrics/job/top/instance/machine

We have now created 2 files named memory-top and cpu-top.

We are going to create 2 .sh files (bash scripts) to keep the scripts running.

Create a file memory.sh and add the below contents

cd /opt
while sleep 1; do ./memory-top; done;

Create a file named cpu.sh and add the below contents

cd /opt
while sleep 1; do ./cpu-top; done;

Make sure the bash scripts are executable by running,

chmod +x cpu.sh && chmod +x memory.sh

By this time , We will have four files under /opt directory

Now we need to setup a program under supervisor to manage these bash scripts.

cd /etc/supervisor/conf.d

Create a file named cpu.conf and add the below configurations.

[program:cpu]
command=sh /opt/cpu.sh
autostart=true
autorestart=true
stderr_logfile=/var/log/long.err.log
stdout_logfile=/var/log/long.out.log

Create a file named memory.conf and add the below configurations.

[program:memory]
command=sh /opt/memory.sh
autostart=true
autorestart=true
stderr_logfile=/var/log/long.err.log
stdout_logfile=/var/log/long.out.log

Save and close the file.

We need to run the below commands to let the supervisor to reread the configuration file and apply the latest changes.

supervisorctl reread
supervisorctl update

To run the supervisor client , Run

supervisorctl

To check the status of the process , Run

status

You should get the below response.

Monitor Linux Processes

We can also check the status of the programs from the linux bash by running,

supervisorctl status

We can manage each programs from the supervisor client.

Now the bash scripts will keep collecting the cpu and memory usage of top running Linux processes.

These scripts will be managed by supervisor service.

To list all the available commands of supervisor client,

Supervisor on Ubuntu

We can manage each programs by running

command programname
Supervisor on Ubuntu
Supervisor on Ubuntu

Configure Supervisor Webserver Client

To allow access to supervisord webserver ,

Open the /etc/supervisor/supervisord.conf file and go to [inet_http_server] section.

Uncomment all the four lines and configure as shown below.

[inet_http_server]
port=*:9001
username=give_username
password=give_password

Dont forget to replace give_username and give_password with the desired credentials.

Save and close the file.

We need to restart the supervisord service for the changes to take effect.

systemctl restart supervisord

We can access the supervisor webservice interface using the ip address of the server and the port 9001.

http://IPaddress:9001

It will ask for username and password , Once authenticated.

You will see the following screen.

Supervisor on Ubuntu

From this interface we can manage the programs.

Conclusion

We have successfully installed and configured supervisor on the Centos and also we have learnt to manage programs using supervisor service.

Hope you find it helpful.Please check out my other articles.