Using Supervisor for Process Monitoring and Management on Linux
This article provides a comprehensive guide to installing, configuring, and using Supervisor—a client/server process monitoring tool—to reliably manage persistent background services on Linux, covering basic usage, advanced features, command-line and web interfaces, and automatic restart mechanisms.
Supervisor is a client/server process monitoring and management tool for UNIX-like systems that ensures the stability of persistent background processes such as API services or consumers.
The article outlines typical deployment scenarios where traditional nohup cmd & fails to guarantee process stability, and introduces Supervisor as a solution.
1. Overview
Supervisor monitors target processes and automatically restarts them upon unexpected termination, supporting grouped management of multiple processes.
2. Installation
<code>pip install supervisor</code>Note: Supervisor does not support Windows.
3. Configuration
Generate a base configuration file:
<code>echo_supervisord_conf > /etc/supervisord.conf</code>Key sections include [unix_http_server] , [supervisord] , and [supervisorctl] , with parameters for socket files, logging, and process definitions.
Custom service configuration files can be included via the [include] directive, allowing modular management of multiple projects.
Application Configuration Example
<code>[program:test]
command=python -u ./test.py
directory=/root/test/
redirect_stderr=true
stdout_logfile=/root/test/test.log</code>Link the application config into the include directory:
<code>ln ./supervisor-test.ini /etc/supervisord.d/supervisor-test.ini</code>4. Running Supervisor
Start the daemon:
<code>supervisord</code>Control processes with supervisorctl (e.g., status , start , stop , restart , signal ).
5. Advanced Features
Process Group Management
<code>[group:test]
programs=test-task_service, test-collector</code>Commands can target groups using the syntax groupname: .
Program Parameter Details
command : command to run.
directory : working directory.
numprocs and process_name : run multiple instances.
autostart : start on daemon launch.
stdout_logfile , stdout_logfile_maxbytes , stdout_logfile_backups : log handling.
redirect_stderr : merge stderr into stdout.
Supervisorctl Commands
Supported commands include add , remove , status , start , stop , restart , signal , update , etc.
Signal Handling in Applications
<code>import time, signal
RUN = True
def exit_handler(signum, frame):
print(f'processing signal({signal.Signals(signum).name})')
print('update task status')
print('clear cache data')
global RUN
RUN = False
signal.signal(signal.SIGTERM, exit_handler)
while RUN:
print(time.strftime('%Y-%m-%d %H:%M:%S'))
time.sleep(1)
print('exited')</code>Supervisor can send signals to managed processes (e.g., signal 15 test ) to trigger graceful shutdown logic.
Web Interface
<code>[inet_http_server]
port=0.0.0.0:9001
username=user
password=123</code>After enabling and restarting, the web UI provides visual process control (ensure it is not exposed publicly).
6. Automatic Restart Mechanism
The autorestart option controls restart behavior ( true , false , unexpected ), while exitcodes defines expected exit codes. startsecs determines the minimum runtime for a process to be considered successfully started, and startretries limits restart attempts.
7. Summary
Supervisor offers a robust solution for managing persistent services, with flexible configuration, group management, signal handling, and both CLI and web interfaces, making it suitable for DevOps and operations workflows.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.