Supervisord
简介
Supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具。它可以很方便的监听、启动、停止、重启一个或多个进程,并守护这些进程。
作用
在项目中,Supervisor用于监控和守护主要逻辑服务和其他服务,如neo4j、redis服务
安装
使用yum安装supervisor
yum install supervisor -y
配置
默认配置文件路径:/etc/supervisor/supervisord.conf
任务配置通常存放在 /etc/supervisor/conf.d
目录,在该目录下,可以创建多个配置文件指示 Supervisor 如何监视进程
也可以将配置文件单独提出到项目进行修改
cd /xxx/xxx/
cp /etc/supervisor/supervisord.conf .
配置python逻辑服务
[program:xxx]
command=/root/miniconda3/envs/xxx/bin/python wr.py
directory=/root/xxx/
stopsignal=QUIT ; signal used to kill process (default TERM)
stopasgroup=false ; send stop signal to the UNIX process group (default false)
killasgroup=false ; SIGKILL the UNIX process group (def false)
stdout_logfile=/var/log/xxx_out ; stdout log path, NONE for none; default AUTO
stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
stderr_logfile=/var/log/xxx_error ; stderr log path, NONE for none; default AUTO
stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
两个关键命令
command需要执行的命令。如python命令,可以通过which python找到
directory命令执行的目录。
配置gunicorn+flask服务
[program:main_serve]
command=/root/miniconda3/envs/xxx/bin/gunicorn -w 1 -b 0.0.0.0:5000 app:app ; the program (relative uses PATH, can take args)
directory=/root/xxx/xxx/main_serve
stopsignal=QUIT ; signal used to kill process (default TERM)
stopasgroup=false ; send stop signal to the UNIX process group (default false)
killasgroup=false ; SIGKILL the UNIX process group (def false)
stdout_logfile=/var/log/xxx_out ; stdout log path, NONE for none; default AUTO
stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
stderr_logfile=/var/log/xxx_error ; stderr log path, NONE for none; default AUTO
stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
配置neo4j
[program:neo4j]
command=neo4j console
user=neo4j
autostart=true
autorestart=unexpected
startsecs=30
startretries=999
priorities=90
exitcodes=0,1,2
stopsignal=SIGTERM
stopasgroup=true
killasgroup=true
redirect_stderr=true
stdout_logfile=/var/log/neo4j/neo4j_supervisor.out
stdout_logfile_backups=10
stderr_capture_maxbytes=20MB
注意:
command是neo4j console而非neo4j start
配置neo4j需要修改/var/log/neo4j/和/var/lib/neo4j目录属组,否则无法启动
chown -R neo4j:neo4j /var/log/neo4j
chown -R neo4j:neo4j /var/lib/neo4j
官方说明:https://neo4j.com/developer/kb/using-supervisord-to-manage-neo4j-process/
使用
启动
- 使用配置文件在/etc/supervisord.conf
# 启动,启动时会同时启动管理的任务
systemctl start supervisord
# 停止,停止时会同时停止管理的任务
systemctl stop supervisord
# 可以设置开机启动
systemctl enable supervisord
# 取消开机启动
systemctl disable supervisord
# 注意,使用supervisord管理neo4j需要取消neo4j开机启动,否则会和suporvisord冲突。
systemctl disable neo4j
- 使用配置文件在自定义目录
# 使用supervisord命令,读取指定目录下的配置文件
supervisord -c /xxx/xxx/supervisord.conf
# 同时取消开机启动
systemctl disable supervisord
其他常用命令
# 查看所有服务
supervisorctl status all
# 进入supervisord命令行
supervisorctl
# 在supervisord命令行中输入
status # 查看管理服务状态
stop xxx # 关闭xxx服务
start xxx # 启动xxx服务
端口占用情况处理
# 通过yum安装lsof命令
sudo yum install lsof -y
# 查看5000端口的进程
lsof -i:5000
# kill杀死占用端口的进程(如,pid为123456)
kill -9 123456
心得:配置文件虽然涉及参数众多,但了解掌握了常用的几个关键参数含义和配置套路,便可以轻松使用