1,安装supervisor
yum install supervisor
2,编辑配置文件,存储目录/etc/supervisord.d/xxx.ini,可配置多个
#python 项目示例
[program:demotest]
command=python demotest.py 10000 ; 被监控的进程启动命令
directory=/root/ ; 执行前要不要先cd到目录去,一般不用
priority=1 ;数字越高,优先级越高
numprocs=1 ; 启动几个进程
autostart=true ; 随着supervisord的启动而启动
autorestart=true ; 自动重启。。当然要选上了
startretries=10 ; 启动失败时的最多重试次数
exitcodes=0 ; 正常退出代码(是说退出代码是这个时就不再重启了吗?待确定)
stopsignal=KILL ; 用来杀死进程的信号
stopwaitsecs=10 ; 发送SIGKILL前的等待时间
redirect_stderr=true ; 重定向stderr到stdout
#laravel队列示例
[program:default-worker]
process_name=%(program_name)s_%(process_num)02d
command=/usr/local/php/bin/php /home/wwwroot/admin.xxxxx.com.au/artisan queue:work redis --tries=2
autostart=true
autorestart=true
user=www
numprocs=4
redirect_stderr=true
stdout_logfile=/home/wwwlogs/jobs/default-worker.log
[program:long-worker]
process_name=%(program_name)s_%(process_num)02d
command=/usr/local/php/bin/php /home/wwwroot/admin.xxxxx.com.au/artisan queue:work redis --tries=2 --timeout=600
autostart=true
autorestart=true
user=www
numprocs=1
redirect_stderr=true
stdout_logfile=/home/wwwlogs/jobs/long-worker.log
#调整/etc/supervisord.conf,加载所有配置文件
[include]
files = supervisord.d/*.ini
3,启动supervisor
/usr/bin/supervisord
4,开启所有监控
/usr/bin/supervisorctl start all
### 查看supervisorctl支持的命令
# supervisorctl help
default commands (type help <topic>):
=====================================
add exit open reload restart start tail
avail fg pid remove shutdown status update
clear maintail quit reread signal stop version
### 查看当前运行的进程列表
# supervisorctl status
#update 更新新的配置到supervisord(不会重启原来已运行的程序)
#reload,载入所有配置文件,并按新的配置启动、管理所有进程(会重启原来已运行的程序)
#start xxx: 启动某个进程
#restart xxx: 重启某个进程
#stop xxx: 停止某一个进程(xxx),xxx为[program:theprogramname]里配置的值
#stop groupworker: 重启所有属于名为groupworker这个分组的进程(start,restart同理)
#stop all,停止全部进程,注:start、restart、stop都不会载入最新的配置文
#reread,当一个服务由自动启动修改为手动启动时执行一下就ok
5,设置开机启动,在目录/usr/lib/systemd/system/ 新建文件supervisord.service,并添加配置内容
# dservice for systemd (CentOS 7.0+)
# by ET-CS (https://github.com/ET-CS)
[Unit]
Description=Supervisor daemon
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf #默认安装,-c选项可省略
ExecStop=/usr/bin/supervisorctl shutdown
ExecReload=/usr/bin/supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
#启用开机启动
systemctl enable supervisord
#检测是否生效
systemctl is-enabled supervisord
6,监控虚拟环境conda中的服务
#command示例
command=bash -c "source /path_to_conda/bin/activate env_name && program_to_run --config=config_path command"
#以gunicorn为例
command=bash -c "source /home/xxx/anaconda3/bin/activate flask && gunicorn -w 2 -b 0.0.0.0:1800 --access-logfile /home/xxx/mybin/flaskapp/logs/access.log 'wsgi:app'"