使用supervisor来守护你的进程

去年在开发公司一个Steam饰品交易平台时遇到过一个处理机器人的问题,当时的机器人太多,但是程序只启动了一个进程去跑就遇到了很多很奇怪的问题,比如说不同机器人的cookie串了,这导致货品出现在严重问题,后来想了一个方法,为每个机器人开一个进程,但是这样的话管理起来很麻烦,当时公司cto用c写了一个进程守护程序,使用一个配置文件解决了管理的问题。现在了解到其实可以使用supervisor来进行管理。

supervisor的作用
supervisor是用python写的一款守护进程的工具,方便的管理需要守护进行的程序。

安装:
pip install supervisor

使用:
安装好supervisor后,命令行会多出来一个命令,supervisorctl,这个是用来管理你的supervisor的,常用的命令有

supervisorctl status 查看状态
supervisorctl reload 重新加载配置文件

修改配置:
配置文件在/etc/supervisor/supervisor.conf这里,默认配置如下:

; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)

; the below 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: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

; 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/supervisor/conf.d/*.conf  ;这里是加载的其它配置文件

配置文件中我们需要注意的是inclue节中的配置文件后缀,这决定了你的配置文件后缀名。
然后是logfile=/var/log/supervisor/supervisord.log 运行日志的目录,这个文件用来查找错误。

编写自己应用的配置文件:

[program:test]
autorestart=True 
autostart=True
redirect_stderr=True
command=php 1.php  ;这里是要运行的命令
user=root
directory=/var/www/super ; 这里是命令运行的目录
stdout_logfile = /var/www/super/test.log ;命令输出目录
;[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)

编写好配置文件后使用上面所说的重新加载配置文件就可以了supervisorctl reload

做一个小测试:

root@homestead:/var/www/super# ps aux | grep php
root      1354  0.0  2.3 480008 49044 ?        Ss   02:03   0:00 php-fpm: master process (/etc/php/7.1/fpm/php-fpm.conf)
root      1365  0.0  1.0 349300 21352 ?        Ss   02:03   0:00 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)
www-data  1640  0.0  0.3 349300  7460 ?        S    02:03   0:00 php-fpm: pool www
www-data  1641  0.0  0.3 349300  7460 ?        S    02:03   0:00 php-fpm: pool www
www-data  1755  0.0  0.4 480008  9432 ?        S    02:03   0:00 php-fpm: pool www
www-data  1756  0.0  0.4 480008  9432 ?        S    02:03   0:00 php-fpm: pool www
root      6224  0.0  0.9 202840 19048 ?        S    08:18   0:00 php 1.php
root      6227  0.0  0.0  14228   912 pts/1    S+   08:18   0:00 grep --color=auto php
root@homestead:/var/www/super# kill -9 6224
root@homestead:/var/www/super# ps aux | grep php
root      1354  0.0  2.3 480008 49044 ?        Ss   02:03   0:00 php-fpm: master process (/etc/php/7.1/fpm/php-fpm.conf)
root      1365  0.0  1.0 349300 21352 ?        Ss   02:03   0:00 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)
www-data  1640  0.0  0.3 349300  7460 ?        S    02:03   0:00 php-fpm: pool www
www-data  1641  0.0  0.3 349300  7460 ?        S    02:03   0:00 php-fpm: pool www
www-data  1755  0.0  0.4 480008  9432 ?        S    02:03   0:00 php-fpm: pool www
www-data  1756  0.0  0.4 480008  9432 ?        S    02:03   0:00 php-fpm: pool www
root      6228  0.0  0.9 202840 19080 ?        S    08:18   0:00 php 1.php
root      6230  0.0  0.0  14228   912 pts/1    S+   08:18   0:00 grep --color=auto php

可以看到,当我们把6224的进程杀死后,supervisor又为我们启动了一个进程号为6228的新进程。

你可能感兴趣的:(使用supervisor来守护你的进程)