Supervisor is a general-purpose process management program developed in Python, primarily used to turn a regular command-line process into a background daemon, monitoring the process status and automatically restarting it in case of abnormal termination.
Installation Prerequisites
The Linux server must have Python installed.
Installation
yum install supervisor
The configuration file for Supervisor is located at /etc/supervisord.conf, which contains the configuration options for the main service command and monitoring management commands of Supervisor.
The directory for other custom business configuration files for Supervisor is by default in the /etc/supervisord.d folder, in .ini format.
The format of the business configuration file is as follows:
[program:think-queue] # think-queue is the business startup namecommand = /usr/bin/php /data/www/think queue:work --daemon # Command to start the program; # Automatically starts when supervisord starts;autostart = true # Automatically restart the program after abnormal exit;autorestart = true # If there is no abnormal exit after 5 seconds of startup, it is considered to have started normally;startsecs = 5 # Number of automatic retries on startup failure, default is 3;startretries = 3 # User to start the program;user = nobody # Redirect stderr to stdout, default is false;redirect_stderr = true # Standard log output;stdout_logfile=/data/log/memcached/out-memcache.log # Error log output;stderr_logfile=/data/log/memcached/err-memcache.log # Standard log file size, default 50MB;stdout_logfile_maxbytes = 20MB # Number of backups for the standard log file;stdout_logfile_backups = 20
Starting Supervisor will launch the think-queue process.
supervisord -c /etc/supervisord.conf
After Supervisor starts, three files will be generated in the /tmp directory: supervisord.log, supervisord.pid, and supervisor.sock. If there are issues, you can check the logs.
Check if Supervisor has started
$ ps -ef | grep supervisor | grep -v grep
root 1170 1 0 18:57 ? 00:00:00 /usr/bin/python /usr/bin/supervisord
Check if the business process has been launched
$ supervisorctl status
think-queue RUNNING pid 1230, uptime 0:04:39
Stopping Supervisor
Note: Child processes will also be stopped.
supervisorctl shutdown
Common Commands
Query the running status of each process
supervisorctl status
Reload configuration
Note: All processes will stop and restart, exercise caution in production environments.
supervisorctl reload
Restart processes based on the latest business configuration
Note: This command will start new configurations or processes with changes based on the latest configuration file, while processes without changes will not be affected and restarted, making it safe for production operations.
supervisorctl update
Start, stop, or restart business processes, where think-queue is the process name, i.e., the value configured in [program:think-queue]
supervisorctl start think-queue
supervisorctl stop think-queue
supervisorctl restart think-queue
Start, stop, or restart all processes
Note: The latest configuration file will not be loaded.
supervisorctl start all
supervisorctl stop all
supervisorctl restart all
Common Pitfalls
1. Use absolute paths for the command to start the program, such as /usr/bin/php.
command = /usr/bin/php /data/www/think queue:work –daemon
2. The user to start the program must match the user of the program code or have higher permissions.
user = nobody