Supervisor Process Monitoring and Management Guide
This article introduces Supervisor, a client/server process monitoring tool for Unix-like systems, explains its installation, configuration, and usage—including custom service and application files, command-line control with supervisorctl, advanced features like process groups, automatic restart policies, and web UI—providing practical examples and code snippets for reliable daemon management.
Supervisor is a client/server process monitoring and management tool for Unix-like operating systems. It monitors target processes, automatically restarts them on unexpected termination, and supports grouping multiple processes for unified control.
1. Overview
Supervisor solves stability issues for persistent background processes such as API services or consumers.
2. Problem Scenario
Typical deployment uses nohup cmd & , which cannot guarantee stable long‑running processes; Supervisor addresses this limitation.
3. Supervisor Introduction
Supervisor is a client/server system that allows its users to control a number of processes on UNIX‑like operating systems.
Key features include state monitoring, automatic restart, and group management.
4. Deployment Process
4.1 Install Supervisor
Install via pip:
<code>pip install supervisor</code>Note: Supervisor does not support Windows.
4.2 Create Service Configuration
Generate a base config file:
<code>echo_supervisord_conf > /etc/supervisord.conf</code>Key sections such as [unix_http_server] , [supervisord] , and [supervisorctl] define socket paths, logging, and PID files.
4.3 Create Application Config
For a test project with test.py , create supervisor-test.ini :
<code>[program:test]
command=python -u ./test.py
directory=/root/test/
redirect_stderr=true
stdout_logfile=/root/test/test.log</code>Link it into the include directory:
<code>ln ./supervisor-test.ini /etc/supervisord.d/supervisor-test.ini</code>4.4 Start supervisord
Run the daemon:
<code>supervisord</code>Specify a config file with -c if needed.
4.5 Use supervisorctl
Interact with the daemon:
<code>supervisorctl</code>Check status, start, stop, restart processes, and view logs with tail -f .
5. Advanced Features
5.1 Process Group Management
Define groups using [group:mygroup] and list member programs.
<code>[group:test]
programs=test-task_service, test-collector
[program:test-task_service]
command=python -u ./task_service.py
[program:test-collector]
command=python -u ./collector.py</code>5.2 Program Configuration Parameters
command : command to run.
directory : working directory.
numprocs : number of process instances.
autostart : start on supervisord launch.
stdout_logfile , stdout_logfile_maxbytes , stdout_logfile_backups : log handling.
redirect_stderr : merge stderr into stdout.
5.3 supervisorctl Commands
Use help to list commands; common ones include status , start , stop , restart , signal , update , reload , and shutdown .
5.4 Signal Handling in Applications
Python example registers a handler for SIGTERM to perform cleanup before exit.
<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>5.5 Web UI
Enable [inet_http_server] in /etc/supervisord.conf to access a web interface on port 9001.
6. Simple Analysis of Auto‑Restart Mechanism
The autorestart option controls whether supervisord restarts a process after exit. Values:
autorestart=unexpected (default): restart on non‑expected exit codes.
autorestart=true : always restart, even on normal exit.
autorestart=false : never restart.
Combined with exitcodes , startsecs , and startretries , you can fine‑tune restart behavior.
7. Conclusion
Supervisor provides a robust solution for managing persistent processes, supporting custom configurations, process groups, automatic restarts, signal handling, and optional web UI, making it valuable for reliable service deployment and operations.
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.