Python-based process management tool supervisor User Guide, pythonsupervisor

Source: Internet
Author: User

Python-based process management tool supervisor User Guide, pythonsupervisor

Supervisor is a Python-based process management tool and can only run on Unix-Like systems, that is, it cannot run on Windows. The Supervisor official version can only run on Python 2.4 or later, but it cannot run on Python 3, but there is already a Python 3 porting supervisor-py3k.

Under what circumstances do we need Process Management? It is to execute some programs that need to be executed in the form of daemon. For example, a background task is most commonly used to start and manage Web programs written based on Tornado.

In addition, the Supervisor can also easily manage the logs output by the Program on the command line, redirect the logs to custom log files, and split the logs by file size.

The Supervisor has two main components:

  1. Supervisord starts a process supervisord when running the Supervisor. It is responsible for starting the managed process and starting the managed process as its own sub-process, in addition, it can be automatically restarted when the managed process crashes.
  2. Supervisorctl is a command line management tool that can be used to execute commands such as stop, start, and restart to manage these sub-processes.

Install

sudo pip install supervisor

Create a configuration file

echo_supervisord_conf > /etc/supervisord.conf

If you have no permission, use this command.

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

Configuration File description

To learn how to configure the process to be managed, you only need to open supervisord. conf, which contains detailed comments.

Open the configuration file

vim /etc/supervisord.conf

The default configuration file is as follows, but there is a pitfall to note, supervisord. pid and supervisor. sock is stored in the/tmp directory, but the/tmp directory stores temporary files. The files in the directory will be deleted by the Linux system. Once these files are lost, the restart and stop commands can no longer be executed through supervisorctl. Only unix: // tmp/supervisor will be obtained. sock does not exist.

[Unix_http_server]; file =/tmp/supervisor. sock; (the path to the socket file); change it to the/var/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. to the/var/log directory to avoid being deleted by the system. log; (main log file; default $ CWD/supervisord. log) logfile_maxbytes = 50 MB; (max main logfile bytes b4 rotation; default 50 MB) logfile_backups = 10; (num 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); change to the/var/run directory to avoid being deleted by the system. pid; (supervisord pidfile; default supervisord. pid )...; set the user to start supervisord. Generally, do not start it with the root user unless you are sure to do so. user = chrism; (default is current user, required if root )... [supervisorctl]; it must match the settings in 'unix _ http_server '; serverurl = unix: // tmp/supervisor. sock; use a unix: // URL for a unix socket; change to the/var/run directory to avoid serverurl = unix: // var/run/supervisor being deleted by the system. 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; shocould be same as http_username if set; password = 123; shocould be same as http_password if set...

By default, when the process log file reaches 50 MB, it is split and up to 10 files are retained. Of course, these configurations can also be configured separately for each process.

Permission problems

After setting the configuration file, you should first create the new folder in the above configuration file. If the user to be started is specified, take oxygen as an example. Pay attention to the permission issues of related files, including log files. Otherwise, an error with no permission may occur. For example, if the startup user oxygen gen is set and supervisord is started, an error occurs.

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

In the preceding configuration file, the/var/run Folder does not grant the write permission to oxygen, the user who starts supervisord. The/var/run folder is actually linked to/run, so we modify the/run permission.

sudo chmod 777 /run

This is a bit simple and rude. You can also consider modifying the. sock,. pid and other files in the above configuration file to other folders, and ensure that you have the corresponding permissions. In general, we can use the root user to start the supervisord process, and then specify the user to start these processes in the processes managed by the root user.

Use a browser to manage

Supervisor also provides a browser to manage processes. You only need to comment out 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

 

Use include

At the end of the configuration file, there is a [include] configuration item. Like Nginx, You can include all the configuration files in a folder, in this way, we can write a separate file for the configuration of each process or several related processes.

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

Process configuration example

A simple example is as follows:

; Set the process name. When using supervisorctl to manage a process, use the process name [program: your_program_name] command = python server. py -- port = 9000; numprocs = 1; default value: 1; process_name = % (program_name) s; default value: % (program_name) s, that is, [program: x] In xdirectory =/home/python/tornado_server; before executing the command, switch to the working directory user = oxygen; start the process using the oxygen Gen user; automatically restart when the program crashes, the number of reboots is limited. The default value is 3 autorestart = true redirect_stderr = true; stdout_logfile =/var/log/supervisord/tornado_server.logloglevel = info

Set Log Level

Loglevel specifies the log level. Logs output using the print Statement of Python are not recorded in the log file. You need to use the logging module of Python to output logs with a specified level.

Multiple processes

According to the definition in the official document, a [program: x] actually represents a group of processes with the same features or similar types. That is to say, 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. When using supervisorctl to manage a process, use the process name [program: foo]; you can use a python expression to pass different parameters to the command = python server. py -- port = 90% (process_num) 02 ddirectory =/home/python/tornado_server; switch to the working directory before executing command; If numprocs is not 1, process_name expressions 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 the process autorestart = true; redirect_stderr = true is automatically restarted when the program crashes; stdout_logfile =/var/log/supervisord/tornado_server.logloglevel = info

In the preceding example, two processes are started. process_name is foo: foo_01 AND foo: foo_02, respectively. In this way, you can use a [program: x] configuration item to start a group of very similar processes.

Two other configuration items are introduced.Stopasgroup and killasgroup

The default value is false. If it is set to true, when the process receives the stop signal, it will automatically send the signal to the sub-process of the process. If this parameter is set to true, killasgroup is also implicitly set to true. For example, when Flask is used in Debug mode, Flask does not pass the received stop signal to its sub-process. Therefore, you need to set this configuration item.

Stopasgroup = false; send stop signal to the UNIX process; the default value is false. If it is set to true, when the process receives the kill signal, it automatically sends the signal to the sub-process of the process. If this program uses python's multiprocessing, It can automatically stop its subthreads. Killasgroup = false; SIGKILL the UNIX process group (def false)

For more detailed configuration examples, refer to the following.

;[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

The Supervisor also provides another way to manage process groups. In this way, you can use the supervisorctl command to manage a group of processes. Different from the process group of [program: x], the processes here are one by one [program: x].

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

After the above configuration is added, the process names of progname1 and progname2 will be changed to thegroupname: progname1 and thegroupname: progname2. Later, you will use this name to manage the process, instead of the previous progname1.

Run supervisorctl stop thegroupname: to end progname1 and progname2 at the same time, and run supervisorctl stop thegroupname: progname1 to end progname1. The supervisorctl command will be introduced later.

Start supervisord

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

# Use the default configuration file/etc/supervisord. confsupervisord # specify the configuration file supervisord-c/etc/supervisord. conf # Use the user to start supervisordsupervisord-u user

For more parameters, see

Supervisorctl command Introduction

# Stop a process. The value of program_name is [program: x] xsupervisorctl stop program_name # start a process supervisorctl start program_name # restart a process supervisorctl restart program_name # End all processes that belong to the groupworker group (similarly to start and restart) supervisorctl stop groupworker: # End groupworker: name1 (similarly to start and restart) supervisorctl stop groupworker: name1 # stop all processes. Note: start, restart, and stop do not load the latest configuration file supervisorctl stop all # load the latest configuration file, stop the original process and start and manage all processes based on the new configuration. supervisorctl reload # Start new or modified processes based on the latest configuration file, processes with no configuration changes will not be affected, and supervisorctl update will be restarted.

Note: The process stopped with stop is displayed. It will not be restarted automatically with reload or update. You can also refer to here

Automatically Start Supervisord upon startup

Supervisord is not installed as a service by default. It is also a process. The official website has provided a script to install Supervisord as a service. You can refer to the installation scripts for various operating systems here, but I cannot run the Ubuntu script provided by the official website.

For the installation method, refer to the answer on serverfault.

For example, if I am a Ubuntu system, I can install it like this. Another script is selected here.

# Download the 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 run sudo chmod + x/etc/init. d/supervisord # Set to boot automatically run sudo update-rc.d supervisord defaults # Try, whether it works normally service supervisord stopservice supervisord start

Note: After downloading this script, you also need to check whether it is consistent with our configuration, such as the default configuration file path and pid file path, if there are differences, you need to make some modifications.

In fact, there is also a simple method, because Linux will execute the script in/etc/rc. local at startup, so you only need to add the execution command here.

# Add/usr/local/bin/supervisord-c/etc/supervisord to Ubuntu. conf # Add/usr/bin/supervisord-c/etc/supervisord for Centos. conf

The preceding content must be added before the exit command. Because PATH environment variables are not fully initialized when the rc. local script is executed, the absolute PATH must be used for the command.

Before adding a command, test whether the command can be executed normally on the terminal. If no supervisord is found, use the following command to find the command:

sudo find / -name supervisord

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.