supervisord process management services

Brief introduction

Official website http://supervisord.org/
Supervisor is to allow a user to monitor and control processes in linux operating system the number of client / server systems. Written by python language to run the monitoring server, identify problems immediately automatic warning and automatic restart functions. supervisor program also requires non-daemon program management, supervisord will help you turn it into a daemon program.

Package

  • supervisord
    service side, he was responsible for starting a child process under his own process, the corresponding client command from the restart crash or abnormal exit of a child process, export-related logs, be generated and managed for a time to the child active during
    profile location: /etc/supervisord.conf pay attention to a reasonable allocation of rights

  • supervisorctl
    command line client, there is a shell-like interface to supervisord provided, through which users can connect to different supervisorctl process, view, start and stop the child, the child lists running processes. Interact through TCP, providing authentication, unified configuration [supervisorctl] section

  • Server web
    supervisorctl of web management interface, by accessing http: // localhost: 9001 / manage sub-process state, [inet_http_server] This configures

installation

Prerequisite is to have a python environment, linux environment generally comes with python, ubuntu here for an example.
1.apt installation
apt-get install -y supervisor
2.easy_install installation
easy_install supervisor

start up

systemctl start supervisor

Configuration file parsing

Generating a configuration file:
echo_supervisord_conf > /tmp/supervisord.conf
Usually after installation profile apt default location is /etc/supervisor/supervisord.conf. Which at the beginning of the comment is a semicolon
[unix_http_server] # This file is initiated by socket web server, this must be, because the command line supervisorctl is achieved through this.
= /tmp/supervisor.sock File
the chmod 0777 =
chown = the nobody: nogroup
username = User
password = 123

[inet_http_server] # initiated via network ports Server Web
Port = 127.0.0.1:9001
username = User
password = 123

[supervisord] # this is a service configuration
logfile = /tmp/supervisord.log log file
logfile_maxbytes = 50MB maximum log file size
logfile_backups = number of backup log at 10 polling
loglevel = info log level
pidfile = /tmp/supervisord.pid pid file position
nodaemon = false If true, supervisor will start at the front end
minfds = 1024 supervisord successful start of the minimum number of file descriptors
minprocs = 200 supervisord minimal promoter success of the process descriptor of the number of
the umask = 022
user start = chrism user, to be noted that this , the user must have the appropriate permission to the directory
identifier = supervisor supervisor process identifier string, the user interface to the RPC protocol
directory = / tmp when supervisord service daemonizes, switch to this directory, the usable% (here) s variable to extend to the entire profile
nocleanup = true prohibit any empty AUTO sub supervisord log files exist in the start time
childlogdir = / tmp AUTO from the log file directory
strip_ansi = false remove all ANSI escape sequences in the sub-log file
environment = KEY1 = "value1", KEY2 = "value2" a list of key / value of an environment variable, right?

[supervisorctl]
serverurl = unix:///tmp/supervisor.sock
username = chris
password = 123
prompt = mysupervisor String used as supervisorctl prompt.作为supervisorctl提示字符串。

Configuration management process

Configuration file location: /etc/supervisor/conf.d/
generally have the following configuration items:
process_name =% (program_name) S # process name, program name is the default
command launcher command
numprocs = 1 # number of processes
directory = / tmp # Path
umask = 022 # mask
priority = 999 # priority, the greater is opened sooner
autorestart = true # automatic restart
startsecs = 10 # start wait time (sec)
startretries # start =. 3 retries
stopsignal = TERM # Close signal
stopwaitsecs = # 10 before closing latency
user = chrism # monitor user rights
redirect_stderr = false # redirect error output
stdout_logfile = / a / path # redirect input log
stdout_logfile_maxbytes = 1MB # log size
stdout_logfile_backups = 10 # log backup
stdout_capture_maxbytes = 1MB
stderr_logfile = / A / path
stderr_logfile_maxbytes = 1MB
stderr_logfile_backups = 10
= 1MB stderr_capture_maxbytes
Environment. 1 = A =, B = 2 # predefined environment variable
serverurl = AUTO # URL system

Examples

In order to better display, I am here to write a simple service. The following specific code:

#!/usr/bin/env python
import socket

HOST, PORT = '', 8080

listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind((HOST, PORT))
listen_socket.listen(1)
while True:
    client_connection, client_address = listen_socket.accept()
    request = client_connection.recv(1024)
    http_response = """\
HTTP/1.1 200 OK
   Hello World!
"""
    client_connection.sendall(http_response)
    client_connection.close()

This script is a simple web server, port 8080, all access will return Hello World!

Here we look at the profile /etc/supervisor/conf.d/webserver.conf


[program:webserver]
autostart=true
startretries=3
command=/opt/webserver.py

View Status

root@0c1fc23d1398:~# supervisorctl status
webserver                        RUNNING   pid 1120, uptime 0:08:07

supervisorctl command

1. interactive mode
directly into supervisorctl to enter interactive mode.

root@0c1fc23d1398:~# supervisorctl 
webserver                        RUNNING   pid 1120, uptime 0:09:40
supervisor> 

2. The command line
above mentioned section ofsupervisorctl status

3. Specific command item
interaction and have the same command line mode.
reread the configuration being reloaded
update the configuration file of the child process to join the new process group, if you set the autostart = true will start the new new child process
status to view the status of all processes
status to view the status of the specified process
start all start all child processes
start starts the specified child process
restart all reset all child processes
restart to restart the specified child process
stop all stop all child processes
stop stops the specified child process
reload restart supervisord
the Add to add the child process group
reomve process to remove the child from the group, we need to stop. Note: After removing the need to use reread and update in order to run the process again

web server interface

Remember the "profile analysis" on a web server configuration section do? I can not remember to go back to look at. We show slightly lower web interface. Browser and enter http: // <ip>: <port>, will pop pop input user name and password, enter the information can see the interface.


13371820-0c9893bb6ec7d3ba.PNG
web interface

You can see from the interface, the status of a service, process id and run out of time. There are a few simple operations, such as restart, stop, clear the log, log monitoring and so on.

Common mistakes

1. Start Multi-process
given the information as follows: Starting supervisor: Error:% ( process_num) must be present within process_name when numprocs> 1 in section 'program: nginx' (file: '/etc/supervisor/conf.d/nginx.conf ')
the For Help, use / usr / bin / supervisord -H

This is not the profile format, the format is the following profile
when time numprocs = 1, process_name =% (program_name) s
when numprocs> = time 2,% (program_name) s _ % (process_num)

Now the following

[program:sleeptime]
autostart=true
startretries=3
command=/opt/sleeptime.py
#主要是下面的2行
process_name=%(program_name)s%(process_num)s
numprocs=4

This time viewing process status, you will find four processes occur

root@0c1fc23d1398:/etc/supervisor/conf.d# supervisorctl status
sleeptime:sleeptime0             RUNNING   pid 1139, uptime 0:00:04
sleeptime:sleeptime1             RUNNING   pid 1140, uptime 0:00:04
sleeptime:sleeptime2             RUNNING   pid 1141, uptime 0:00:04
sleeptime:sleeptime3             RUNNING   pid 1142, uptime 0:00:03
webserver                        RUNNING   pid 1120, uptime 0:30:00

2. run directly supervisorctl status report:
Error: Server authentication The requires
the For Help, use / usr / local / bin / supervisorctl -h
because you set the password to access the account, we can only go in the first supervisorctl in status.

Mainly to see the log information, and log the program itself.

Reproduced in: https: //www.jianshu.com/p/57f951a58d47

Guess you like

Origin blog.csdn.net/weixin_34293902/article/details/91091830