Keeping Services Running 24/7 with Supervisor

Supervisor is a process management tool used for starting, stopping, restarting, and monitoring processes. It is capable of automatically relaunching processes when they crash.


To install Supervisor, run the following command:

sudo pip3 install supervisor


First, generate the configuration file by running echo_supervisord_conf > /etc/supervisord.conf.

The generated default configuration file requires minimal modifications. The only change needed is to uncomment the [include] section at the end of the file and add a custom include path rule:


Program Configuration

Let's take an example of configuring a frontend proxy service developed in Python. If you want this service to run continuously without interruption, you can create a configuration file named /etc/supervisor/my_service.conf with the following content:

; my_service.conf

directory = /opt/my_service
command = /usr/local/bin/python3
autostart = true
startsecs = 5
autorestart = true
startretries = 300
user = root
redirect_stderr = true
stdout_logfile_maxbytes = 100MB
stdout_logfile_backups = 10
stdout_logfile = /opt/my_service/log/info.log

For a new program, you generally only need to modify the directory, command, user, and stdout_logfile configuration options.

Starting the Services

Execute supervisord -c /etc/supervisord.conf to start the Supervisor daemon. Then, run supervisorctl to enter the interactive interface. The interface will display the monitored process services and immediately launch any newly configured services. You can simply exit the interactive interface by pressing Ctrl+D without any additional operations.

Auto-Start on Boot

Edit the /etc/rc.local file and add the following command:

/usr/local/bin/supervisord -c /etc/supervisord.conf

This ensures that Supervisor automatically starts after a reboot, and Supervisor, in turn, automatically relaunches all the monitored services.

How to Restart a Service

To restart a service, find its process ID (PID) using the ps command and then kill the monitored service. Supervisor will automatically relaunch the service once it is killed. If you wish to prevent automatic relaunching of the service (which is generally not required), you need to kill the Supervisor process first.

2022-12-08 08:10:14 | NOTE | 0 Comments


Leave A Comment