在 Linux 系统安装 Supervisor 来管理进程

服务介绍

Supervisor 是用 Python 开发的一个 client/server 服务,是 Linux/Unix 系统下的一个进程管理工具,不支持 Windows 系统。它可以很方便的监听、启动、停止、重启一个或多个进程。用 Supervisor 管理的进程,当一个进程意外被杀死,supervisort 监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。

安装python(3.8)

1、下载并解压

1
wget https://www.python.org/ftp/python/3.8.10/Python-3.8.10.tar.xz

安装编译器:

1
yum -y install gcc

安装make需要的依赖包:

1
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

安装python3.8需要的依赖包,否则会报错:“ModuleNotFound:No module named ‘_ctypes’”

1
yum -y install libffi-devel

进入解压后的文件夹中,编译和安装(如果之前安装出错的朋友,在安装依赖后重新编译安装即可):

1
2
3
cd Python-3.8.10
./configure --prefix=/usr/local/python3
make && makeinstall

安装Supervisord

1、使用pip安装

1
pip3 install supervisor

2.生成配置文件

1
echo_supervisord_conf > /etc/supervisord.conf

启动关闭 启动:

1
supervisord -c /etc/supervisord.conf

关闭:

1
supervisorctl shutdown

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[program:xx]
command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run; 程序启动命令
directory=/opt/screenCron/logs/
autostart=true; 在supervisord启动的时候也自动启动
startsecs=10; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
autorestart=true; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
startretries=10; 启动失败自动重试次数,默认是3
priority=1; 进程启动优先级,默认999,值小的优先启动
redirect_stderr=false; 把stderr重定向到stdout,默认false
stderr_logfile_maxbytes=50MB; 默认50MB,stderr_logfile的最大值
stderr_logfile=/opt/screenCron/logs/
stdout_logfile_maxbytes=50MB; stdout 日志文件大小,默认50MB
stdout_logfile=/opt/screenCron/logs/ ; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
stopasgroup=false;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=false;默认为false,向进程组发送kill信号,包括子进程

[program:]
command=
directory=/opt/screenCron/logs/
autostart=true
startsecs=10
autorestart=true
startretries=10
priority=1
redirect_stderr=false
stderr_logfile_maxbytes=50MB
stderr_logfile=/opt/screenCron/logs/
stdout_logfile_maxbytes=50MB
stdout_logfile=/opt/screenCron/logs/

相关管理命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
supervisorctl status 查看进程运行状态

supervisorctl restart program_name 重启

supervisorctl start program_name 进程名 启动进程

supervisorctl stop program_name 进程名 关闭进程

supervisorctl restart 进程名 重启进程

supervisorctl update 重新载入配置文件

supervisorctl shutdown 关闭

supervisord supervisordctl clear 进程名 清空进程日志

supervisordctl 进入到交互模式下。使用help查看所有命令。

start stop restart + all 表示启动,关闭,重启所有进程。

supervisorctl stop all 全部停止

相关文档

1
2
3
4
5

https://www.kancloud.cn/adapa/gopher/1271861
https://www.jianshu.com/p/7e788634257b
https://blog.csdn.net/qq_34896760/article/details/80513206
https://www.cnblogs.com/xuezhigu/p/7660203.html