English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Supervisor usage guide based on Python process management tool

Supervisor is a process management tool based on Python and can only run on Unix-Like's system, that is, it cannot run on Windows. The official version of Supervisor can only run on Python 2.4 version above, but it still cannot run on Python 3 However, there is already a Python 3 of the移植版 supervisor-py3k.

When do we need process management? It is to execute programs that need to be executed as daemons, such as a background task, and the most commonly used one is to start and manage web programs written based on Tornado.

In addition, Supervisor can also manage the logs output by programs in a very friendly way, can redirect logs to custom log files, and can split logs by file size.

Supervisor has two main components:

  1. When running Supervisor, a process called supervisord is started, which is responsible for starting the managed processes and starting the managed processes as its own subprocesses, and can automatically restart the managed processes if they crash.
  2. supervisorctl is a command-line management tool that can be used to execute stop, start, restart, and other commands to manage these subprocesses.

Installation

sudo pip install supervisor

Create the configuration file

echo_supervisord_conf > /etc/supervisord.conf

If you encounter a permission issue, you can use this command

sudo su - root -c "echo_supervisord_conf > /etc/supervisord.conf"

Configuration file description

If you want to know how to configure the processes to be managed, just open supervisord.conf, which contains very detailed comment information.

Open the configuration file

vim /etc/supervisord.conf

The default configuration file is as follows, but there is a pit to note that supervisord.pid and supervisor.sock are placed in /in the tmp directory, but /The tmp directory is used to store temporary files, and the files inside will be deleted by the Linux system. Once these files are lost, you can no longer execute the restart and stop commands through supervisorctl, and you will only get unix:///tmp/The error that supervisor.sock does not exist.

[unix_http_server]
;file=/tmp/supervisor.sock  ; (the path to the socket file)
; modify to /var/The 'run' directory, to avoid being deleted by the system
;file=/var/run/supervisor.sock  ; (the path to the socket file)
;chmod=0700         ; socket file mode (default 0700)
;chown=nobody:nogroup    ; socket file uid:gid owner
;username=user       ; (default is no username (open server))
;password=123        ; (default is no password (open server))
;[inet_http_server]     ; inet (TCP) server disabled by default
;port=127.0.0.1:9001    ; (ip_address:port specifier, *:port for ;all iface)
;username=user       ; (default is no username (open server))
;password=123        ; (default is no password (open server))
...
[supervisord]
;logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)
; modify to /var/The 'log' directory, to avoid being deleted by the system
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB    ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10      ; (number of main logfile rotation backups;default 10)
loglevel=info        ; (log level;default info; others: debug,warn,trace)
;pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
; modify to /var/The 'run' directory, to avoid being deleted by the system
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
...
;Set the user to start supervisord, generally do not use the root user to start unless you are really sure you want to do so
;user=chrism         ; (default is current user, required if root)
...
[supervisorctl]
; must match the settings in 'unix_http_server'
;serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
; modify to /var/The 'run' directory, to avoid being deleted by the system
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris       ; should be same as http_username if set
;password=123        ; should be same as http_password if set
...

By default, the log file of the process reaches5When it reaches 0MB, it will be split, and at most retain10files, of course, these configurations can also be configured separately for each process.

Permission issue

After setting up the configuration file, you should first create the new directories mentioned in the configuration file. If a startup user is specified, taking oxygen as an example, you should pay attention to the permission issues of related files, including log files, otherwise an error of no permission will occur. For example, if the startup user oxygen is set, and then supervisord is started, an error will occur

Error: Cannot open an HTTP server: socket.error reported errno.EACCES (13)

It is due to the configuration file above /var/The 'run' directory does not have write permission granted to the user oxygen for starting supervisord./var/The run folder is actually a link to /Since we have no run, we modify /run permission.

sudo chmod 777 /run

This is a bit simple and rough, and you can also consider changing the .sock, .pid, and other files in the above configuration file to other folders and ensure the corresponding permissions. Generally, we can start the supervisord process with the root user and then specify which user to start these processes in the processes managed by it.

Manage using the browser

Supervisor also provides a method to manage processes through a browser, just uncomment the following lines.

;[inet_http_server]     ; inet (TCP) server disabled by default
;port=127.0.0.1:9001    ; (ip_address:port specifier, *:port for ;all iface)
;username=user       ; (default is no username (open server))
;password=123        ; (default is no password (open server))
[supervisorctl]
...
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris       ; should be same as http_username if set
;password=123        ; should be same as http_password if set

 

Using include

At the end of the configuration file, there is an [include] configuration item. Like Nginx, it can include all configuration files in a folder, so we can write the configuration for each process or a few related processes separately into a file.

[include]
files = /etc/supervisord.d/*.ini

Process configuration example

A simple example is as follows

; Set the process name, which is required when managing processes with supervisorctl
[program:your_program_name] 
command=python server.py --port=9000
;numprocs=1         ; 默认为1
;process_name=%(program_name)s  ; 默认为 %(program_name)s,即 [program:x] 中的 x
directory=/home/python/tornado_server ; Switch to the working directory before executing the command
user=oxygen         ; Use the oxygen user to start this process
; Automatically restart the program when it crashes, and the number of restarts is limited, the default is3time
autorestart=true      
redirect_stderr=true    ; Redirect the output logs
stdout_logfile = /var/log/supervisord/tornado_server.log
loglevel=info

Set log level

loglevel specifies the log level. The logs output by Python's print statement will not be recorded in the log file, and it needs to be paired with Python's logging module to output logs with specified levels.

Multiple processes

According to the definition in the official documentation, a [program:x] actually represents a group of processes with the same features or categories, which means that a [program:x] can start multiple processes. The members of this group of processes are determined by the numprocs and process_name parameters. What does this mean? Let's look at this example.

; Set the process name, which is required when managing processes with supervisorctl
[program:foo] 
; Different parameters can be passed to each process using python expressions here
command=python server.py --port=90%(process_num)02d
directory=/home/python/tornado_server ; Switch to the working directory before executing the command
; If numprocs is not1The expression of process_name must contain process_num to distinguish different processes
numprocs=2          
process_name=%(program_name)s_%(process_num)02d; 
user=oxygen         ; Use the oxygen user to start this process
autorestart=true      ; Automatically restart the program when it crashes
redirect_stderr=true    ; Redirect the output logs
stdout_logfile = /var/log/supervisord/tornado_server.log
loglevel=info

In this example, two processes will be started, with process_name as foo:foo_01 and foo:foo_02In this way, a [program:x] configuration item can be used to start a group of very similar processes.

Here are two configuration items introduced stopasgroup and killasgroup

; Default is false, if set to true, when the process receives a stop signal, it will automatically send the signal to the subprocesses of the process. If this configuration item is set to true, it also implies that killasgroup is true. For example, when using Flask in Debug mode, Flask will not pass the received stop signal to its subprocesses, so this configuration item needs to be set.

stopasgroup=false       ; send stop signal to the UNIX process 
; Default is false, if set to true, when the process receives a kill signal, it will automatically send the signal to the subprocesses of the process. If this program uses python's multiprocessing, it can automatically stop its subprocesses.
killasgroup=false       ; SIGKILL the UNIX process group (def false)

More detailed configuration examples can be found as follows, official documentation is here

;[program:theprogramname]
;command=/bin/cat       ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1          ; number of processes copies to start (def 1)
;directory=/tmp        ; directory to cwd to before exec (def no cwd)
;umask=022           ; umask for process (default None)
;priority=999         ; the relative start priority (default 999)
;autostart=true        ; start at supervisord start (default: true)
;autorestart=unexpected    ; whether/when to restart (default: unexpected)
;startsecs=1          ; number of secs prog must stay running (def. 1)
;startretries=3        ; max # of serial start failures (default 3)
;exitcodes=0,2         ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT        ; signal used to kill process (default TERM)
;stopwaitsecs=10        ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false       ; send stop signal to the UNIX process group (default false)
;killasgroup=false       ; SIGKILL the UNIX process group (def false)
;user=chrism          ; setuid to this UNIX account to run the program
;redirect_stderr=true     ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path    ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB  ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10   ; # of stdout logfile backups (default 10)
;stdout_capture_maxbytes=1MB  ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false  ; emit events on stdout writes (default false)
;stderr_logfile=/a/path    ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB  ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10   ; # of stderr logfile backups (default 10)
;stderr_capture_maxbytes=1MB  ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false  ; emit events on stderr writes (default false)
;environment=A="1",B="2"    ; process environment additions (def no adds)
;serverurl=AUTO        ; override serverurl computation (childutils)

Manage multiple processes by group

Supervisor also provides another way to manage process groups, through which you can manage a group of processes using the supervisorctl command. Unlike [program:x] process groups, the processes here are individual [program:x].

[group:thegroupname]
programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions
priority=999         ; the relative start priority (default 999)

After adding the above configuration, progname1 and progname2 The process name will become thegroupname:progname1 and thegroupname:progname2 In the future, this name will be used to manage the process instead of the previous progname1.

From now on, executing supervisorctl stop thegroupname: will be able to end progname at the same time1 and progname2,execute supervisorctl stop thegroupname:progname1 it can end progname1.We will introduce the supervisorctl commands later.

Start supervisord

Executing the supervisord command will start the supervisord process, and the processes set in the configuration file will also start accordingly.

# Use the default configuration file /etc/supervisord.conf
supervisord
# Explicitly specify the configuration file
supervisord -c /etc/supervisord.conf
# Start supervisord using user
supervisord -u user

For more parameters, please refer toDocumentation

Supervisorctl command introduction

# Stop a specific process, program_name is x in [program:x]
supervisorctl stop program_name
# Start a process
supervisorctl start program_name
# Restart a process
supervisorctl restart program_name
# End all processes belonging to the group named groupworker (start, restart similarly)
supervisorctl stop groupworker:
# End groupworker:name1 This process (start, restart similarly)
supervisorctl stop groupworker:name1
# Stop all processes, note: start, restart, stop will not load the latest configuration file
supervisorctl stop all
# Load the latest configuration file, stop the original processes and start, manage all processes according to the new configuration
supervisorctl reload
# Start new configuration or modified processes based on the latest configuration file, processes that have not been modified will not be affected and restarted
supervisorctl update

Note: The process stopped with stop will not restart automatically with reload or update. You can also refer toHere

Automatically start Supervisord on boot

Supervisord is not installed as a service by default, and it is also a process itself. The official documentation has provided scripts that can install Supervisord as a service. You can refer to here to view installation scripts for various operating systems, but the Ubuntu script provided by the official here cannot be run.

Installation methods can be referred to serverfault .

For example, if I am using an Ubuntu system, I can install it like this; another script is chosen here

# Download script
sudo su - root -c "sudo curl https://gist.githubusercontent.com/howthebodyworks/176149/raw/d60b505a585dda836fadecca8f6b03884153196b/supervisord.sh > /etc/init.d/supervisord
# Set this script to be executable
sudo chmod +x /etc/init.d/supervisord
# Set to run automatically at startup
sudo update-rc.d supervisord defaults
# Try to see if it works normally
service supervisord stop
service supervisord start

Note: After downloading this script, you still need to check whether it is consistent with our configuration, such as the default configuration file path, pid file path, etc. If there are differences, some modifications need to be made.

Actually, there is an easier method, because Linux will execute /etc/The script in rc.local, so you can add the execution command here

# If it is Ubuntu, add the following content
/usr/local/bin/supervisord -c /etc/supervisord.conf
# If it is Centos, add the following content
/usr/bin/supervisord -c /etc/supervisord.conf

The above content needs to be added before the exit command, and since the PATH environment variable is not fully initialized when the rc.local script is executed, the command needs to use the absolute path.

Before adding, test the command in the terminal to see if it can be executed normally. If supervisord cannot be found, you can use the following command to find it

sudo find / -name supervisord

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You may also like