supervisor守护进程工具的安装与配置

supervisor守护进程工具的安装与配置

简介

supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具。可以很方便的监听、启动、停止、重启一个或多个进程。用supervisor管理的进程,当一个进程意外被杀死,supervisor监听到进程死后,会自动将它重启,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。

安装及配置

以CentOS 7.4,IP:192.168.1.100的环境进行supervisor安装,本次介绍三种安装方法。

方法一(建议pip方式):

# wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate   //下载pip
# pip install supervisor   //pip安装supervisro

方法二(easy_install方式):

# yum install python-setuptools 
# easy_install supervisor  //安装supervisor
# easy_install –U supervisor  //更新supervisor

以上两种spervisor安装方法都需要进行以下部分的配置:

1、创建主配文件supervisord.conf

# cd /usr/bin/
# echo_supervisord_conf > /etc/supervisord.conf 

2、创建supervisor配置文件目录/etc/supervisod.d/

# mkdir 755 /etc/supervisord.d/

3、修改配置文件supervisord.conf

; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
;  - Shell expansion ("~" or "$HOME") is not supported.  Environment
;    variables can be expanded using this syntax: "%(ENV_HOME)s".
;  - Quotes around values are not supported, except in the case of
;    the environment= options as shown below.
;  - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
;  - Command will be truncated if it looks like a config file comment, e.g.
;    "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ".

[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   ;开户HTTP服务器,提供web管理界面
port=192.168.0.100:9001        ; ip_address:port specifier, *:port for all iface   ;输入服务器IP地址及Web管理端口
username=admin             ; default is no username (open server)   //设置登录管理后台的用户名
password=SupervisorPassword               ; 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=supervisord            ; setuid to this UNIX account at startup; recommended 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://192.168.0.100:9001 ; use an http:// url to specify an inet socket   ;通过http的方式连接supervisord
username=admin              ; should be same as in [*_http_server] if set   ;设置登录管理后台的用户名
password=SupervisorPassword                ; 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                   ; 'expected' exit codes used with autorestart (default 0)
;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)
;stdout_syslog=false           ; send stdout to syslog with process name (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)
;stderr_syslog=false           ; send stderr to syslog with process name (default false)
;environment=A="1",B="2"       ; process environment additions (def no adds)
;serverurl=AUTO                ; override serverurl computation (childutils)

; The sample eventlistener section below shows all possible eventlistener
; subsection values.  Create one or more 'real' eventlistener: sections to be
; able to handle event notifications sent by supervisord.

;[eventlistener:theeventlistenername]
;command=/bin/eventlistener    ; 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)
;events=EVENT                  ; event notif. types to subscribe to (req'd)
;buffer_size=10                ; event buffer queue size (default 10)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=-1                   ; the relative start priority (default -1)
;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        ; autorestart if exited after running (def: unexpected)
;exitcodes=0                   ; 'expected' exit codes used with autorestart (default 0)
;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=false         ; redirect_stderr=true is not allowed for eventlisteners
;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_events_enabled=false   ; emit events on stdout writes (default false)
;stdout_syslog=false           ; send stdout to syslog with process name (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_events_enabled=false   ; emit events on stderr writes (default false)
;stderr_syslog=false           ; send stderr to syslog with process name (default false)
;environment=A="1",B="2"       ; process environment additions
;serverurl=AUTO                ; override serverurl computation (childutils)

; The sample group section below shows all possible group values.  Create one
; or more 'real' group: sections to create "heterogeneous" process groups.

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

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

[include]
files = /etc/supervisord.d/*.ini   ;设置守护进程的配置文件路径,可以指定一个或多个以.ini结束的配置文件

注,修改每一项配置,需要去掉每行前注释“;”。

4、设置开机启动Supervisor服务

# vi /usr/lib/systemd/system/supervisord.service   //新建supervisord服务

添加以下内容:

[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service nss-user-lookup.target

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
ExecStop=/usr/bin/supervisord shutdown
ExecReload=/usr/bin/supervisord reload
killMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

supervisor添加到开机启动:

# systemctl enable supervisord.service   //设置spervisor开机启动
# systemctl is-enabled supervisord.service  //验证supervisor是否为开机启动,出现enabled表明设置成功。

5、启动服务、查看版本及状态

# systemctl start supervisord.service  //启动supervisor服务
# supervisord  -version    //查看supervisor版本,当前最新版本是4.0.4
# systemctl restart supervisord   //重启supervisor服务
# systemctl status supervisord   //查看supervisor状态

方法三(yum方式):

# yum install epel-release
# yum -y install supervisor   //安装supervisor
# supervisord  -version   //查看supervisor版本,当前版本是3.1.4(非最新版本)
# systemctl enable supervisord   //设置spervisor开机启动
# systemctl restart supervisord   //重启supervisor服务
# systemctl status supervisord   //查看supervisor状态

添加守护进程

其中/etc/supervisord.d/目录用来存放用户自定义的守护进程配置,如添加nginx与php-fpm配置样例如下: nginx.ini

[program:nginx]
command = /usr/sbin/nginx  -c  /etc/nginx/nginx.conf -g 'daemon off;'
stdout_logfile = /data/logs/supervisor/nginx.stdout.log
stderr_logfile = /data/logs/supervisor/nginx.stderr.log
autorestart = true

php-fpm.ini

[program:php-fpm]
command = /usr/local/php/sbin/php-fpm  -c /usr/local/php/etc/php-fpm.conf 
stdout_logfile = /data/logs/supervisor/php-fpm.stdout.log
stderr_logfile = /data/logs/supervisor/php-fpm.stderr.log
autorestart = true

注: supervisor不能监控后台运行的进程,command 不能为后台运行命令,使用supervisor接管进程以后就不可以使用其他命令启动服务。

常用命令介绍

supervisorctl 是 supervisord的命令行客户端工具

supervisorctl restart <application name>  //重启指定应用
supervisorctl stop <application name>  //停止指定应用
supervisorctl start <application name>  //启动指定应用
supervisorctl restart all  //重启所有应用
supervisorctl stop all  //停止所有应用
supervisorctl start all  //启动所有应用
supervisorctl update  //配置文件修改后可以使用该命令加载新的配置
supervisorctl reload  //重新启动配置中的所有程序

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×