An overview of the use of supervisor

Preface

In the past, in order not to be killed, we used to run code with nohup + &. Later, we found that the system thought that all the damn programs had to die, which was very depressing. There are many tools for process daemons on the market, but they are written in different languages (for example, pm2, written in node language). supervisor is written in python, which used to support py2, and now py3 is also supported, so we don't need to worry about the version. I'd like to introduce some regular use here. By the way, if you have any questions, please refer to the official website: Official website of supervisor

I. installation

There are various installation methods,

  • Offline installation: (the company's internal network is not connected to the external network) you need to download the installation package in advance, compile and install it with make and make install. It's a little more complicated. Let's not talk about it here. If you need to do it yourself.
  • Online installation: because the supervisor is written by python, it can be installed directly by pip install supervisor, or by yum install supervisor. pip is the first mode.

Two. Configuration

All startup settings of the supervisor are configured in one file, which is the only and most important one.

  • Generate basic configuration file: run echo supervisor conf command to redirect to a file
echo_supervisord_conf > /home/flask_pro/supervisor_config/supervisord.conf

/Home / flash_pro / this directory is the directory of a flash project I built,
Supervisor config is a directory used to store the configuration and log files of supervisor,
Without redirection, a supervisor.conf file will be generated under / etc / by default
  • The file of supervisor.conf indicates that the semicolon (;) is the annotation symbol, which means that this line does not work. There are many items below, which are mainly introduced
[unix_http_server]
file=/tmp/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
logfile_maxbytes=50MB        ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10           ; # of main logfile backups; 0 means none, default 10
loglevel=info                ; log level; default info; others: debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false               ; start in foreground if true; default false
minfds=1024                  ; min. avail startup file descriptors; default 1024
minprocs=200                 ; min. avail process descriptors;default 200
;umask=022                   ; process file creation umask; default 022
;user=chrism                 ; default is current user, required if root
;identifier=supervisor       ; supervisord identifier, default is 'supervisor'
;directory=/tmp              ; default is not to cd during start
;nocleanup=true              ; don't clean up tempfiles at start; default false
;childlogdir=/tmp            ; 'AUTO' child log dir, default $TEMP
;environment=KEY="value"     ; key value pairs to add to environment
;strip_ansi=false            ; strip ansi escape codes in logs; def. false

; The rpcinterface:supervisor section must remain in the config file for
; RPC (supervisorctl/web interface) to work.  Additional interfaces may be
; added by defining them in separate [rpcinterface:x] sections.

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

; The supervisorctl section configures how supervisorctl will connect to
; supervisord.  configure it match the settings in either the unix_http_server
; or inet_http_server section.

[supervisorctl]
serverurl=unix:///tmp/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 in [*_http_server] if set
;password=123                ; should be same as in [*_http_server] if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history  ; use readline history if available

; The sample program section below shows all possible program subsection values.
; Create one or more 'real' program: sections to be able to control them under
; supervisor.

;[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)
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
;startretries=3                ; max # of serial start failures when starting (default 3)
;autorestart=unexpected        ; when to restart if exited after running (def: unexpected)
;exitcodes=0,2                 ; 'expected' exit codes used with autorestart (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 (0 means none, 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 (0 means none, 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)

;[include]
;files = relative/directory/*.ini

Focus on several project configurations

  1. Communication mode selection
[unix_http_server]
file=/tmp/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)

The two above provide two communication modes, sockt and TCP,
socket mode is secure. TCP mode can have an external access interface,
To select a socket, you need to configure the file item, specify the path where the socket is stored, and use the default value
 I choose TCP, mainly for an interface. The configuration results are as follows:

;[unix_http_server]
;file=/tmp/supervisor.sock
;chmod=0700
;chown=nobody:nogroup
;username=user
;password=123

[inet_http_server]
port=0.0.0.0:9001; the default configuration can only be accessed locally. If it is 0.0.0.0, the monitoring interface can be viewed externally
username=user
password=123
  1. The [supervisor] service configuration has no change. Except for logfile and pidfile, I have changed the path. By default, it's a matter of writing logs and recording processes. What should be studied
logfile=/home/flask_pro/supervisor_config/configfiles/supervisord.log
pidfile=/home/flask_pro/supervisor_config/configfiles/supervisord.pid
  1. [rpcinterface:supervisor] don't worry about this project, default
  2. [supervisor CTL] service is very important. It is required for starting, restarting, shutting down and querying
[supervisorctl]
; serverurl=unix:///tmp/supervisor.sock; this is to choose socket communication mode. We chose TCP before, so we commented out
 serverurl=http://0.0.0.0:9001; change the settings of TCP communication according to the above settings
 username=user; consistent with the above TCP communication mode
 password=123; consistent with the above TCP communication mode et

All other configurations are default. In fact, other configurations are useless
  1. [program: the programname] process setting, I directly give a chestnut that I write, so it is clear
[program: circle UU test]; process name: circle UU test
 command = /root/anaconda2/bin/python -u circle_test.py; command to run
 Directory = / home / flash_pro / Pro /; the folder where the command is run
 Stdout? Logfile = / home / flash? Pro / Pro / circle? Test.log; log print file location
 autostart = true; set whether to start the process automatically after startup
 startsecs = 5; set a few seconds after startup to start the process
 autorestart = true; whether to restart automatically
 startretries = 3; it will not restart after three restarts. To be clear, it will be three times at most, and it will not work in the future
 Stdout ﹣ logfile ﹣ maxbytes = 100MB; the maximum log capacity, if it exceeds, reopen the log file at the end of the label
  1. [include] include relationship configuration
;[include]
;files = relative/directory/*.ini
 In fact, it can be understood that our supervisor.conf also includes daemons of other files, which can be added together
 For example:
[include]
files = /etc/supervisor.conf

Three. Use

Now that the configuration is complete, I will show the circle_test.py file of step 5 above

#!/usr/bin/python
# -*- coding: utf-8 -*-
import time

if __name__ == '__main__':
    count = 0
    while True:
        print count, 'sleep 1s'
        count = count + 1
        time.sleep(1)

supervisor startup options

 supervisord -c /home/flask_pro/supervisor_config/supervisord.conf
 If there is no error printing, the startup is basically successful
 We can also take a look at circle ou test.log
 [root@cmsoft pro]# tail -f circle_test.log 
	19982 sleep 1s
	19983 sleep 1s
	19984 sleep 1s
	19985 sleep 1s
	19986 sleep 1s
	19987 sleep 1s
 There's no problem with the process
 You can also use the command
supervisorctl -c /home/flask_pro/supervisor_config/supervisord.conf status
 See:
circle_test      RUNNING   pid 13335, uptime 5:45:21
 The process has been started for 5h45min21s
 -C / home / flash ﹣ Pro / Supervisor ﹣ config / supervisor.conf is the specified configuration file parameter

Then we can see the process monitoring through the interface: Browser input linux ip:9001

After entering, you can see:

You can stop, restart and other process operations through the interface operation

4, Key commands

Supervisor CTL stop program \ name stop a process
 Supervisor CTL start program \ name start a process
 Supervisor CTL restart program \ name restart a process

All parameter means all, eg:
Supervisor CTL stop all stops all processes

Supervisor CTL reload loads the latest configuration file, stops the original process and starts according to the new configuration
 Supervisor CTL update starts a new configuration or a changed process based on the latest configuration file

In fact, the above commands need to add a configuration file option. See step (3. Use),
If the parameter is not configured, the / etc / supervisor.conf configuration file will be used by default

Five. Conclusion

The above is just a brief description of the common configuration items. If you need to further study, you must see the latest documents.

77 original articles published, 47 praised, 110000 visitors+
Private letter follow

Tags: supervisor socket Unix Python

Posted on Sun, 02 Feb 2020 22:09:09 -0800 by Fizzgig