Supervisor is a useful utility to manage processes, applications, scripts and tasks in Linux. It allows you to start, stop, restart, update processes in Linux. It is easy to use, simple, efficient, and used widely among businesses. In this article, we will look at how to install supervisor in RHEL/CentOS/Fedora. It consists of two components – supervisord and supervisorctl. supervisord is the main server that runs as a daemon allowing you to start programs, restart crashed processes, exit tasks, and managing events during process lifetime. supervisorctl is the command line interface that allows you to issue commands to supervisord.
How to Install Supervisor in Ubuntu
Here are the steps to install supervisor in RHEL/CentOS/Fedora.
1. Update System Packages
Open terminal and run the following commands to update system packages.
$ sudo dnf update -y
2. Install EPEL Repository
Run the following command to install EPEL repository.
$ sudo dnf install epel-release
3. Install Supervisor
After you update EPEL repository, run the following commands to install supervisor.
$ sudo yum update $ sudo yum -y install supervisor
4. Start & Enable Supervisor
Run the following command to start & enable supervisord daemon.
$ sudo systemctl start supervisord $ sudo systemctl enable supervisord
5. Check Supervisord Status
Run the following command to check supervisord status.
$ sudo systemctl status supervisord
6. Add Program to Supervisor
Now let us add a shell script to be managed by supervisord. Please note, supervisord supports programs running in foreground only.
Let us assume you have the following shell script.
$ sudo vi /home/ubuntu/process.sh
with the following code
#!/bin/bash while true do # Echo current date to stdout echo `date` # Echo 'error!' to stderr echo 'error!' >&2 sleep 1 done
The above code basically keeps running and echoing the present date every 1 second. Let us make sure it is executable by running the following command.
$ chmod +x /home/ubuntu/process.sh
To add a process to supervisord for managing, you need to add its details in supervisord’s configuration files located at /etc/supervisor/conf.d. Let us create a separate configuration file for our script at /etc/supervisor/conf.d.
$ sudo vi /etc/supervisor/conf.d/process.conf
Add the following lines to it.
[program:long_process] command=/home/ubuntu/process.sh autostart=true autorestart=true stderr_logfile=/var/log/long.err.log stdout_logfile=/var/log/long.out.log
Above the first line is the name for our process. You can refer to your script as long_process while using supervisord. Next, we set the file path to execute this shell script.
Then we set autostart and autorestart to true, so that the script auto starts when system boots, and automatically restarts in case it crashes.
Finally, we specify the location of log files for standard error and standard output.
Save and close the file.
7. Restart Supervisord
Restart Supervisord with the following command.
$ supervisorctl restart
Now if you open the log file for your shell script you will be able to see its output, indicating that your script is running successfully.
$ cat /var/log/long.out.log
Here are some handy commands to help you manage your process using supervisor
Start specific process
$ supervisorctl start <process_name> $ supervisorctl start long_process #example
Stop specific process
$ supervisorctl stop <process_name> $ supervisorctl stop long_process #example
Restart specific process
$ supervisorctl restart <process_name> $ supervisorctl restart long_process #example
Start, stop, restart All processes configured in supervisord
$ supervisorctl start all $ supervisorctl stop all $ supervisorctl restart all
In this article, we have learnt how to install supervisord, supervisorctl, and how to configure a process to be managed by supervisord. We have also learnt a few handy commands to start, stop & restart processes in supervisord. Supervisord is a convenient tool to manage processes in Linux allows you to start, stop & restart processes without remembering their actual commands. It also has the ability to auto restart processes in case they crash, increasing their uptime.
Also read:
How to Install OpenSSL in Ubuntu
How to Reset Root Password in Ubuntu
How to Sort CSV File in Python
How to Read Binary File in Python
How to Empty List in Python
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.