CentOS 安装Supervisor,并监控指定服务状态

1,安装supervisor

yum install supervisor

2,编辑配置文件,存储目录/etc/supervisord.d/xxx.ini,可配置多个

[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
#调整/etc/supervisord.conf,加载所有配置文件
[include]
files = supervisord.d/*.ini

3,启动supervisor

/usr/local/bin/supervisord

4,开启所有监控

/usr/local/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/supervisor/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'"

Leave a Comment

Your email address will not be published.

*

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据