Supervisor进程守护监控

编辑于 2023-11-25 03:23:22 阅读 1027

应用场景

工作中可能要写一些cli脚本,需要后台运行,一般会用 nohup command &

nohup /usr/bin/php /www/test.php >> /var/log/test.log 2>&1 &

但这样会有一些问题,不能监控进程状态,异常退出时不能自动重启,这时候 supervisor 是更好的选择

注意:像nginx, mysql, php-fpm等,还是推荐系统级的systemctl

安装

#centos/redhat/fedora
yum install supervisor

#Debian/Ubuntu可通过apt安装
apt-get install supervisor

#pip安装
pip install supervisor

#easy_install安装
easy_install supervisor

#启动
supervisord -c /etc/supervisor/supervisord.conf

推荐使用 easy_install,安装的版本比较新

[root@iz2zei3sr57xphkhf16csuz ~]# easy_install supervisor
Searching for supervisor
Reading http://mirrors.aliyun.com/pypi/simple/supervisor/
Downloading http://mirrors.aliyun.com/pypi/packages/ce/37/517989b05849dd6eaa76c148f24517544704895830a50289cbbf53c7efb9/supervisor-4.2.5.tar.gz#sha256=34761bae1a23c58192281a5115fb07fbf22c9b0133c08166beffc70fed3ebc12
Best match: supervisor 4.2.5
Processing supervisor-4.2.5.tar.gz
Writing /tmp/easy_install-2AQet8/supervisor-4.2.5/setup.cfg
Running supervisor-4.2.5/setup.py -q bdist_egg --dist-dir /tmp/easy_install-2AQet8/supervisor-4.2.5/egg-dist-tmp-EDB_zo
warning: no previously-included files matching '*' found under directory 'docs/.build'
creating /usr/lib/python2.7/site-packages/supervisor-4.2.5-py2.7.egg
Extracting supervisor-4.2.5-py2.7.egg to /usr/lib/python2.7/site-packages
Adding supervisor 4.2.5 to easy-install.pth file
Installing echo_supervisord_conf script to /usr/bin
Installing pidproxy script to /usr/bin
Installing supervisorctl script to /usr/bin
Installing supervisord script to /usr/bin

Installed /usr/lib/python2.7/site-packages/supervisor-4.2.5-py2.7.egg
Processing dependencies for supervisor
Finished processing dependencies for supervisor

# 创建配置文件
[root@iz2zei3sr57xphkhf16csuz ~]# echo_supervisord_conf

[root@iz2zei3sr57xphkhf16csuz ~]# mkdir /etc/supervisor
[root@iz2zei3sr57xphkhf16csuz ~]# echo_supervisord_conf > /etc/supervisor/supervisord.conf

# 修改主配置
vi /etc/supervisor/supervisord.conf

[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

[include]
files = conf.d/*.ini

配置

新建一个脚本的配置文件

vi /etc/supervisor/conf.d/demo.conf
;demo表示程序名称
[program:demo]
;需要执行的命令
command=php demo.php
;命令执行的目录
directory=/var/www/demo/beanstalkd/demo0/
;环境变量
environment=PATH="/usr/local/bin/"
;哪个用户运行
user=root
;是否自启动
autostart=true
;是否自动重启
autorestart=true
;自动重启时间间隔,单位秒
startsecs=3
;错误日志文件
stderr_logfile=/tmp/demo.err.log
;输出日志文件
stdout_logfile=/tmp/demo.out.log

保存后reload一下,使生效
supervisorctl reload

脚本文件 demo.php

<?php
 
$i = 0;
while(true) {
    $i++;
    echo $i, PHP_EOL;
    sleep(1);
}

web界面

vi /etc/supervisor/supervisord.conf

;增加
[inet_http_server]
port=0.0.0.0:9001
username=admin
password=123456

保存后reload一下,使生效
supervisorctl reload

然后访问 http://localhost:9001/,即可看到如下界面

WX202109261643472x.png

Centos 开机自启

vi /usr/lib/systemd/system/supervisor.service
#supervisor.service
 
[Unit] 
Description=Supervisor daemon
 
[Service] 
Type=forking 
ExecStart=/usr/local/bin/supervisord -c /etc/supervisor/supervisord.conf
ExecStop=/usr/local/bin/supervisorctl shutdown 
ExecReload=/usr/local/bin/supervisorctl reload 
KillMode=process 
Restart=on-failure 
RestartSec=42s
 
[Install] 
WantedBy=multi-user.target

# 激活
systemctl enable supervisor

使用

service supervisor start
service supervisor stop

laravel队列

cat /etc/supervisor/conf.d/laravel-worker.conf
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=docker exec -u www-data environment-php-docker-php-fpm-1 /var/www/laravel-demo/artisan queue:work --queue=high,default,robot,vip_expire --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=root
numprocs=8
redirect_stderr=true
stdout_logfile=/var/log/laravel-worker.log
stopwaitsecs=3600

任务调度(定时任务)

* * * * * docker exec -u www-data environment-php-docker-php-fpm-1 /var/www/laravel-demo/artisan schedule:run >> /dev/null 2>&1

常用命令

#启动
supervisord -c /etc/supervisor/supervisord.conf

#停止supervisord
supervisorctl shutdown

#reload一下,使生效
supervisorctl reload

#查看supervisord当前管理的所有进程的状态
supervisorctl status

#停止supervisord管理的所有进程
supervisorctl stop all
#supervisord管理的某一个特定进程
supervisorctl start program-name // program-name为[program:xx]中的xx
#停止supervisord管理的某一个特定进程
supervisorctl stop program-name  // program-name为[program:xx]中的xx

#重启所有进程或某一进程
supervisorctl restart all //重启所有
supervisorctl reatart program-name //重启某一进程,program-name为[program:xx]中的xx

#启动进程
supervisorctl start xxx
#重启进程
supervisorctl restart xxx
#重启所有属于名为group的分组进程
supervisorctl stop group
#停止全部进程
supervisorctl stop all
#载入最新配置的文件
supervisorctl reload
#根据最新的配置文件,启动新配置或有改动的进程
supervisorctl update
#查看日志文件
/var/log/supervisor

广而告之,我的新作品《语音助手》上架Google Play了,欢迎下载体验