How to use Supervisor in linux

gray laptop computer showing html codes in shallow focus photography
Reading Time: 4 minutes

In Linux, Supervisor is a client/server system that allows its users to monitor and control a number of processes on Linux or UNIX-like operating systems. Whenever we want, Processes can be stopped and started as a unit. It starts its sub-processes via fork/exec. The operating system gives the signals to Supervisor immediately when a process terminates. This is written in Python but Supervisor does not work on Python 3.

Benefits of Supervisor

Convenience:  With the help of the Supervisor, We can configure the process as we want. Like Supervisor can be configured processes to automatically restart when they crash.

Accuracy:  It is quite difficult to get the perfect status of processes about up or down. Basically, it starts the processes as sub-processes, That is the only reason that it is aware of the status of its children’s processes. This is very simple and easy for an end-user.

In this blog, We will learn about the installation of the supervisor and we will configure the supervisor to manage the Nginx process. You can follow the below steps:

Installation of supervisor in ubuntu

Basically, the Supervisor package is already available in Ubuntu‘s repository. But I will show you how to install a supervisor. You can follow the below command: 

sudo apt-get install supervisor -y

After installing Supervisor, We can also verify the version of it by using the below command:

sudo supervisord -v

You will get the version of supervisor in output.

The supervisor is a service therefore we can check the status of supervisor service with this command.

sudo systemctl status supervisor

Supervisor Web Interface

Supervisor also provides a web-based user interface. Users can manage all the processes from there also. But Supervisor’s web interface is disabled by default. You can enable it by editing the file /etc/supervisor/supervisord.conf.

sudo vim /etc/supervisor/supervisord.conf

Add the following lines:

[inet_http_server]
port=*:6000
username=admin #username to access the web interface
password=admin # password to access the web interface

Press esc and :wq then restart the Supervisor service to apply the changes:

systemctl restart supervisor
sudo systemctl status supervisor

Configure the Nginx with Supervisor

First, We need to install the Nginx server with the below command:

sudo apt-get install nginx -y

After installation of Nginx, you will stop nginx and disable the Nginx service. Because here we will use Supervisor to manage the Nginx process.

We will use the below command to stop and disable the Nginx service:

sudo systemctl stop nginx
sudo systemctl disable nginx

Now, we can create an Nginx configuration file:

sudo vim /etc/supervisor/conf.d/nginx.conf

Add the following lines:


[program:nginx]
command=/usr/sbin/nginx -g "daemon off;"
autostart=true
autorestart=true
startretries=5
numprocs=1
startsecs=0
process_name=%(program_name)s_%(process_num)02d
stderr_logfile=/var/log/supervisor/%(program_name)s_stderr.log
stderr_logfile_maxbytes=10MB
stdout_logfile=/var/log/supervisor/%(program_name)s_stdout.log
stdout_logfile_maxbytes=10MB

Explanation:

  • [program:nginx] – Define the program to monitor. We’ll call it “Nginx”.
  • command – Basically, This is used to run the monitored process. 
  • autostart – It means the process will start when the Supervisord starts.
  • auto restart – It means that the process will be restarted if it stops unexpectedly.
  • startretries – It reties the process to a certain count before being considered as failed.
  • stderr_logfile – The file to write any errors output.
  • stdout_logfile – The file to write any regular output.

Press esc and:wq then reread the Supervisor service to apply the changes:

supervisorctl reread

You will get the output as shown below images:

nginx: available

Now update the supervisor to start the Nginx service:

sudo supervisorctl update

You will get the same output as shown below images:

nginx: added process group

Next, verify whether Supervisor started the Nginx service with the following command:

sudo supervisorctl

You should get the following output:

nginx:nginx_00               RUNNING   pid 15717, uptime 0:00:13

If you want to stop the Nginx service the run below command:

stop nginx:nginx_00

Output:

nginx:nginx_00: stopped

To start the Nginx service again then run this command:

start nginx:nginx_00

Output:

nginx:nginx_00: started

If you want to exit from the Supervisor shell then  you can run the below command:

exit

Now Access Supervisor Web Interface

You can now access the Supervisor web page using localhost:9000 or ip:9000 

As you hit the port you will get the login page to access the page. We have added the username and password in the configuration. Therefore we will use that username and password here and get the status of all services affiliated with the supervisor. You can restart and stop the service from here also.

Conclusion

First of all, Thank you for reading my blog. We have covered how to configure the Nginx service with a supervisor.  You can follow the man page of the supervisor.

You can also read my previous blog here.

Written by 

Mohd Muzakkir Saifi is a Software Consultant at Knoldus Software. He loves to take deep dives into cloud technologies & different tools. His hobbies are playing gymnastics and traveling.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading