之前做过一篇 http://blog.caoxl.com/2019/11/06/Supervisor-For-Laravels/, 今天更深入的学习下Supervisor
安装
显示如下,就证明安装成功了
配置&启动
配置
supervisor
的主配置文件在/usr/local/etc/supervisord.conf
;
配置文件在最后一行可以修改新增子配置文件,默认在 /usr/local/etc/supervisor.d/*.ini
也可以 /usr/local/etc/supervisor.d/*.conf
1 2
| [include] files = /usr/local/etc/supervisor.d/*.conf
|
启动supervisord
1
| supervisord -c /usr/local/etc/supervisord.conf
|
1
| brew services start supervisor
|
1 2
| ps -ef | grep supervisord 501 85725 1 0 5:28PM ?? 0:00.78 /usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/Resources/Python.app/Contents/MacOS/Python /usr/local/opt/supervisor/bin/supervisord -c /usr/local/etc/supervisord.conf --nodaemon
|
启动supervisorctl
1 2 3 4
| supervisorctl update
# 或 supervisorctl reload
|
1 2
| # 将all换成相应的program名称,则就是对相应的程序进行启动、关闭和重启 supervisorctl start|stop|restart all
|
实例
以test_shell
为例启动进程
- 编辑配置文件
/usr/local/etc/supervisor.d/shell.conf
1 2 3 4 5 6 7 8 9
| [program:test_shell] process_name=%(program_name)s command=sh /Users/caoxl/WWW/DNMP/www/test.com/shell/test.sh autostart=true autorestart=true user=caoxl numprocs=1 redirect_stderr=true stdout_logfile=/var/log/supervisor/test_shell.log
|
- 立即生效
- 查看结果
1 2
| supervisorctl status test_shell RUNNING pid 86587, uptime 0:00:03
|
program配置文件说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| ;*为必须填写项 ;*[program:应用名称] [program:test_shell]
;当numprocs为1时,process_name=%(program_name)s ;当numprocs>=2时,%(program_name)s_%(process_num)02d process_name=%(program_name)s
;*命令路径,如果使用python启动的程序应该为 python /home/test.py, ;不建议放入/home/user/, 对于非user用户一般情况下是不能访问 command=sh /Users/caoxl/WWW/DNMP/www/test.com/shell/test.sh
;使用supervisor还有一个更大的好处就是,可以快速开启多个进程,配置参数如下: ;进程数量,表示对同一个配置开启1个线程。 numprocs=1
;执行目录,若有/home/supervisor_test/test1.py ;将directory设置成/home/supervisor_test ;则command只需设置成python test1.py ;否则command必须设置成绝对执行目录 directory=/tmp
;掩码:--- -w- -w-, 转换后rwx r-x w-x umask=022
;优先级,值越高,最后启动,最先被关闭,默认值999 priority=999
;如果是true,当supervisor启动时,程序将会自动启动 autostart=true
;*自动重启 autorestart=true
;启动延时执行,默认1秒 startsecs=10
;启动尝试次数,默认3次 startretries=3
;当退出码是0,2时,执行重启,默认值0,2 exitcodes=0,2
;停止信号,默认TERM ;中断:INT(类似于Ctrl+C)(kill -INT pid),退出后会将写文件或日志(推荐) ;终止:TERM(kill -TERM pid) ;挂起:HUP(kill -HUP pid),注意与Ctrl+Z/kill -stop pid不同 ;从容停止:QUIT(kill -QUIT pid) ;KILL, USR1, USR2其他见命令(kill -l),说明1 stopsignal=TERM stopwaitsecs=10
;*以root用户执行 user=caoxl
;有时候用 Supervisor 托管的程序还会有子进程(如 Tornado),如果只杀死主进程,子进程就可能变成孤儿进程。 ;通过这两项配置(改为true)来确保所有子进程都能正确停止,默认是false: stopasgroup=false killasgroup=false
;重定向,把stderr重定向到stdout,默认false; redirect_stderr=true
;标准日志输出 stdout_logfile=/var/log/supervisor/test_shell.log
;标准日志文件大小,默认50MB stdout_logfile_maxbytes=1MB ;标准日志文件大小,默认50MB stdout_logfile_backups=10 stdout_capture_maxbytes=1MB ;标准日志输出 stderr_logfile=/a/path stderr_logfile_maxbytes=1MB stderr_logfile_backups=10 stderr_capture_maxbytes=1MB
;环境变量设置 environment=A="1",B="2" serverurl=AUTO
|
故障处理
ERROR (spawn error)
权限问题,查看下设置的目录权限,日志权限等
- 使用
supervisorctl tail program_name stderr
命令查看错误信息
1 2
| supervisorctl tail test_shell stderr test_shell: ERROR (no log file)
|
supervisor error: class ‘FileNotFoundError’ …
一般使用supervisord -c /usr/local/etc/supervisord.conf
会出现, 仔细检查supervisord.conf
文件里面的子进程配置文件,检查路径有没有问题
supervisor 提示:xxx: ERROR (no such process)
增加了新的配置文件xxx.conf后,使用supervisorctl start xxx
提示 “xxx: ERROR (no such process)”。
使用supervisorctl start all
也不行。
在新增配置文件后,要使用supervisorctl update
命令,使用此命令后会自动加载新的配置,并且启动该进程。
参考