Python-based process management Tools Supervisor Use Guide _python

Source: Internet
Author: User

Supervisor is a Python-based process management tool that can only run on a unix-like system, which means it cannot run on Windows. The official version of Supervisor is currently only running in Python version 2.4, but it's not running on Python 3, but there's already a Python 3 transplant version of SUPERVISOR-PY3K.

Under what circumstances do we need process management? is to perform programs that need to be implemented as daemons, such as a background task, and my most common use is to start and manage Web programs based on Tornado.

In addition, supervisor can also be very friendly to the management program on the command line output of the log, you can redirect the log to the custom log file, but also to the file size of the log segmentation.

The supervisor has two main components:

    1. Supervisord, when you run supervisor, you start a process Supervisord, which is responsible for starting the managed processes and starting the managed processes as their own child processes, and can automatically restart when the managed processes crash.
    2. Supervisorctl is a command-line administration tool that you can use to perform commands such as Stop, start, and restart to manage these child processes.

Installation

sudo pip install supervisor

Creating a configuration file

echo_supervisord_conf > /etc/supervisord.conf

If there is a problem with no permissions, you can use this command

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

Configuration file Description

To understand how to configure the processes that need to be managed, just open the supervisord.conf, which has very detailed annotation information.

Open configuration file

vim /etc/supervisord.conf

The default configuration file is the following, but there's a hole here. Note that Supervisord.pid and Supervisor.sock are placed in the/tmp directory, but the/tmp directory is for temporary files, which are deleted by the Linux system. Once these files are lost, there is no way to execute the restart and stop commands through SUPERVISORCTL, only to get unix:///tmp/supervisor.sock errors.

[Unix_http_server]; file=/tmp/supervisor.sock; (The path to the socket file); modified to/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; (the default is no username (open server));p assword=123; (The default is no password (open server)); [Inet_http_server]; inet (TCP) server disabled by default;p ort=127.0.0.1:9001; (Ip_address:port specifier, *:p ort for; all iface); username=user; (the default is no username (open server));p assword=123;

(Default is no password (open server) ...) [Supervisord]; logfile=/tmp/supervisord.log; (Main log File;default $CWD/supervisord.log); Modify to/var/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; (Num of main logfile Rotation backups;default) Loglevel=info; (log level;default info; others:debug,warn,trace);p idfile=/tmp/supervisord.pid; (Supervisord pidfile;default supervisord.pid); Modify to/var/run directory to avoid being deleted by the system pidfile=/var/run/supervisord.pid; (Supervisord pidfile;default supervisord.pid) ...; Set up the user that starts the Supervisord, generally do not use root user easily to start, unless you are really sure to do so; user=chrism;

(Default is current user, required if root) ... [Supervisorctl]; Must match the setting in ' Unix_http_server '; serverurl=unix:///tmp/supervisor.sock; Use a unix://URL for a UNIX socket; Modify to/var/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 a http://URL to specify a inet socket; Should be same as Http_username if set;p assword=123;

 Should be same as Http_password if set ...

By default, when the log file for a process reaches 50MB, it splits, leaving up to 10 files, which, of course, can be configured individually for each process.

Permission Issues

After you have set up your profile, you should create a new folder in the configuration file above. If you specify a startup user, take oxygen as an example, you should pay attention to the permissions issues associated with the file, including the log file, otherwise there will be an error without permissions. For example, set the start user oxygen, and then start Supervisord an error occurred

Error:cannot Open an HTTP server:socket.error reported errno. Eacces (13)

This is because the/var/run folder in the above configuration file does not grant write access to the user oxygen that started Supervisord. The/var/run folder is actually linked to/run, so we modify the/run permissions.

sudo chmod 777 /run

This is a bit simple and rude, you can also consider modifying files such as. Sock,.pid in the above configuration files to other folders and ensuring that you have the appropriate permissions. In general, we can start the Supervisord process with the root user, and then in the process that it manages, specify the need to start these processes with that user.

Using a browser to manage

Supervisor also provides a way to manage processes through the browser, just to comment out the following lines.

; [Inet_http_server]     ; inet (TCP) server disabled by default
;p ort=127.0.0.1:9001    ; (Ip_address:port specifier, *:p ort for; all iface)
; Username=user       ; (the default is no username (open server))
; Password=123        ; (The default is no password (open server))

[Supervisorctl]
...
; serverurl=http://127.0.0.1:9001; Use a http://URL to specify a inet socket
; Username=chris       should be same as Http_username if set
;p Word=123        should be same as Http_password if set

Using include

At the end of the configuration file, there is an [include] configuration item that, like Nginx, can include all of the configuration files under a folder, so that we can write a separate file for each process or the configuration of several processes involved.

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


Sample configuration for a process

A simple example is as follows

; Sets the name of the process, which is used to manage processes using the Supervisorctl name
[program:your_program_name] 
Command=python server.py--port=9000
; Numprocs=1         ; The default is 1
;p rocess_name=% (program_name) s  ; default is% (Program_name) s, that is, x directory=/home/python/in [program:x]
Tornado_server; Switch to the working directory before executing the command
,         start the process with the oxygen user, User=oxygen
automatically when the program crashes, and the number of restarts is limited, by default 3 times
autorestart= True      
redirect_stderr=true    ; redirect output log
Stdout_logfile =/var/log/supervisord/tornado_server.log
Loglevel=info

Set Log Level

LOGLEVEL Specifies the level of the log, the log output with the Python print statement is not logged to the log file, and a Python logging module is required to output the log at the specified level.

Multiple processes

As defined by the official document, a [program:x] is actually a group of processes that represent a set of identical characteristics or peers, i.e. a [program:x] can start multiple processes. The members of this set of processes are determined by the arguments of Numprocs and process_name, and what does this mean, we look at this example.

; Set the name of the process, use SUPERVISORCTL to manage the process using the process name
[Program:foo] 
; You can pass different parameters to each process in the command here with a Python expression
command= Python server.py--port=90% (process_num) 02d
directory=/home/python/tornado_server; switch to the working directory before executing the command
; If the Numprocs is not 1,process_name, it must contain process_num to differentiate between processes
numprocs=2          
process_name=% (program_name) s_% ( Process_num) 02d; 
User=oxygen         ; Use oxygen user to start the process
autorestart=true      ; automatically restart redirect_stderr=true when the program crashes;    Redirected output log
stdout_logfile =/var/log/supervisord/tornado_server.log
loglevel=info

The above example starts with two processes, Process_name foo:foo_01 and foo:foo_02 respectively. In such a way, you can use a [program:x] configuration item to start a very similar set of processes.

Two more configuration Items Stopasgroup and Killasgroup

; The default is False, and if set to true, the signal is automatically sent to the subprocess of the process when the process receives the stop signal. If this configuration entry is true, then the implied killasgroup is also true. For example, when you use flask in Debug mode, flask does not pass the received stop signal to its child processes, so you need to set this configuration item.

Stopasgroup=false       send stop signal to the UNIX process 

; The default is false, and if set to true, when the process receives a kill signal, it automatically sends the signal to the subprocess of the procedure. If the program uses Python's multiprocessing, it can automatically stop its child threads.
Killasgroup=false       ; SIGKILL the UNIX process group (Def false)

For more detailed configuration examples, you can refer to the following official documentation here

; [Program:theprogramname]; command=/bin/cat; The program (relative uses PATH, can take args);p rocess_name=% (program_name) s; Process_name Expr (default% (program_name) s); Numprocs=1; Number of processes copies to start (Def 1);d irectory=/tmp; Directory to CWD to before exec (Def no cwd); umask=022; Umask for process (default None);p riority=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); Signal used to kill process (default TERM); stopwaitsecs=10; Max num secs to wait b4 SIGKILL (default); 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); 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); 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)

Managing multiple processes by group

Supervisor also provides another way to manage a group of processes, in this way, by using the Supervisorctl command. Unlike the [program:x] process group, the process here is one [program:x].

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

When the above configuration is added, the process names of progname1 and progname2 become thegroupname:progname1 and Thegroupname:progname2 and the name is used to manage the process, not the previous Progname1.

Execute Supervisorctl Stop Thegroupname later: You can end progname1 and progname2 at the same time, execute Supervisorctl stop thegroupname:progname1 to end Progn Ame1. We will introduce Supervisorctl's order later.

Start Supervisord

Executing the Supervisord command will start the Supervisord process, and the process that we set up in the configuration file will start accordingly.

# Use default profile/etc/supervisord.conf
supervisord
# explicitly specify configuration file
supervisord-c/etc/supervisord.conf
# Use User starts Supervisord
supervisord-u user

More parameters Please refer to the documentation

Introduction to the Supervisorctl command

# Stop a process, program_name the x supervisorctl stop program_name # in [program:x]
start a process
supervisorctl start Program_name
# Restarts a process
supervisorctl restart Program_name
# ends all processes that are named Groupworker this grouping (start,restart Similarly)
Supervisorctl stop groupworker:
# End GROUPWORKER:NAME1 This process (Start,restart empathy)
Supervisorctl stop Groupworker:name1
# Stop All processes, note: Start, restart, stop will not load the most recent configuration file
supervisorctl stop all
# Load the latest configuration file, Stop the original process and start and manage all processes according to the new configuration
supervisorctl Reload
# Start a new configuration or a modified process based on the latest configuration file, and the process without changes will not be affected and
restarted SUPERVISORCTL Update

Note: Displays the process that stops with stop and does not automatically restart with reload or update. You can also refer to this

Boot automatically start Supervisord

Supervisord is not installed as a service by default and is itself a process. The authorities have given the script to install Supervisord as a service, refer to the installation scripts for the various operating systems here, but I can't run the Ubuntu script I gave the official here.

The installation method can refer to the answer on the ServerFault.

For example, I'm an Ubuntu system, so I can install it, and I've chosen another script here.

# download Script
sudo su-root-c ' sudo curl https://gist.githubusercontent.com/howthebodyworks/176149/raw/ d60b505a585dda836fadecca8f6b03884153196b/supervisord.sh >/etc/init.d/supervisord "
# Set the script to be able to execute
sudo chmod +x/etc/init.d/supervisord
# set to boot automatically
sudo update-rc.d supervisord defaults
# Try it, work properly
Service Supervisord Stop
service Supervisord start

Note: This script download down, but also need to check with our configuration is consistent, such as the default configuration file path, PID file path, etc., if there are different needs to make some changes.

There is also an easy way to do this, because Linux executes the script inside the/etc/rc.local when it is started, so just add execution commands here to

# if it's Ubuntu add the following content
/usr/local/bin/supervisord-c/etc/supervisord.conf

# If it is Centos add the following
/usr/bin/ Supervisord-c/etc/supervisord.conf

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

Before adding, in the terminal test whether the command can be normal execution, if not found Supervisord, you can use the following command to find

sudo find / -name supervisord

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.