安装进程监控器Supervisor

安装
Supervisor 是 Linux 操作系统下中的一个进程监控器,它可以在 queue:work 挂掉时自动重启之。在 Ubuntu 上安装 Supervisor,你可以使用如下命令:

sudo apt-get install supervisor

配置
[program:erp-goodsin-queue]
user = nobody
command = php cli.php request_uri=/job/erp/goodsinqueue
directory = /var/www/retail
#process_name = msgsend
numprocs = 1
autostart = true
autorestart = true
stdout_logfile = /var/log/supervisor/goodsinqueue.log
stdout_logfile_maxbytes = 10MB
stderr_logfile = /var/log/supervisor/goodsinqueue.error.log
stderr_logfile_maxbytes = 10MB

command=/root/miniconda3/bin/uwsgi –ini uwsgib.ini ; 程序启动命令
directory=/root/xxxx/automobile
autostart=True ; 在supervisord启动的时候也自动启动
startsecs=10 ; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
autorestart=true ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
startretries=3 ; 启动失败自动重试次数,默认是3
user=root ; 用哪个用户启动进程,默认是root
priority=999 ; 进程启动优先级,默认999,值小的优先启动
redirect_stderr=true ; 把stderr重定向到stdout,默认false
stdout_logfile_maxbytes=20MB ; stdout 日志文件大小,默认50MB
stdout_logfile_backups = 20 ; stdout 日志文件备份数,默认是10
; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
stdout_logfile=/root/yangdefeng/touna/automobile/logs/automobile.out
loglevel=debug ;loglevel 指定了日志的级别,用 Python 的 print 语句输出的日志是不会被记录到日志文件中的,需要搭配 Python 的 logging 模块来输出有指定级别的日志。
stopasgroup=true ;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=true ;默认为false,向进程组发送kill信号,包括子进程

管理
root@ebs-26932:~# service supervisor status
● supervisor.service – Supervisor process control system for UNIX
Loaded: loaded (/lib/systemd/system/supervisor.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2019-08-22 02:17:31 UTC; 4s ago
Docs: http://supervisord.org
Process: 22046 ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown (code=exited, status=0/SUCCESS)
Main PID: 22063 (supervisord)
Tasks: 1 (limit: 2319)
CGroup: /system.slice/supervisor.service
└─22063 /usr/bin/python /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf

8月 22 02:17:31 ebs-26932 systemd[1]: Started Supervisor process control system for UNIX.
8月 22 02:17:31 ebs-26932 supervisord[22063]: 2019-08-22 02:17:31,635 CRIT Supervisor running as root (no user in c
8月 22 02:17:31 ebs-26932 supervisord[22063]: 2019-08-22 02:17:31,637 WARN No file matches via include “/etc/superv
8月 22 02:17:31 ebs-26932 supervisord[22063]: 2019-08-22 02:17:31,763 INFO RPC interface ‘supervisor’ initialized
8月 22 02:17:31 ebs-26932 supervisord[22063]: 2019-08-22 02:17:31,764 CRIT Server ‘unix_http_server’ running withou
8月 22 02:17:31 ebs-26932 supervisord[22063]: 2019-08-22 02:17:31,765 INFO supervisord started with pid 22063

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

supervisorctl status:查看所有进程的状态
supervisorctl stop es:停止es
supervisorctl start es:启动es
supervisorctl restart es: 重启es
supervisorctl update :配置文件修改后可以使用该命令加载新的配置
supervisorctl reload: 重新启动配置中的所有程序