重要的进程就让Supervisor 来守护吧!

时间:2022-07-24
本文章向大家介绍重要的进程就让Supervisor 来守护吧!,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

上次给大家介绍了nohup,screen退出终端任务还在,但是一旦我们任务崩溃,这两个命令是没办法维护任务,经大牛张磊提醒,应该使用Supervisor,systemd,PM2(node进程管理工具)来守护进程,粗暴认为崩溃了能重启,这三个之中Supervisor 使用最为普遍,systemd感觉复杂,PM2专用,这次先给大家分享下Supervisor。

目录

1. Supervisor 是什么

2. Supervisor 安装

3. Supervisor 监控shell脚本

4. Supervisor 开机自启动

5. Supervisor 常用命令

1

Supervisor 是什么

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

2

Supervisor 安装

  • centos 7 ,提前装好
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc* make -y
yum install python-setuptools
easy_install supervisor

有可能会失败:
[root@master ~]# easy_install supervisor
Searching for supervisor
Reading https://pypi.python.org/simple/supervisor/
Download error on https://pypi.python.org/simple/supervisor/: [Errno 101] Network is unreachable -- Some packages may not be found!
Couldn't find index page for 'supervisor' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading https://pypi.python.org/simple/
多试试,包很小,网络问题
[root@master ~]# easy_install supervisor
Searching for supervisor
Reading https://pypi.python.org/simple/supervisor/
Best match: supervisor 4.1.0
Downloading https://files.pythonhosted.org/packages/de/87/ee1ad8fa533a4b5f2c7623f4a2b585d3c1947af7bed8e65bc7772274320e/supervisor-4.1.0.tar.gz#sha256=2dc86fe0476e945e61483d614ceb2cf4f93b95282eb243bdf792621994360383

Processing supervisor-4.1.0.tar.gz
Writing /tmp/easy_install-43e91X/supervisor-4.1.0/setup.cfg
Running supervisor-4.1.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-43e91X/supervisor-4.1.0/egg-dist-tmp-rAlpH4
warning: no previously-included files matching '*' found under directory 'docs/.build'
Adding supervisor 4.1.0 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.1.0-py2.7.egg
Processing dependencies for supervisor
Finished processing dependencies for supervisor
  • yum直接安装
yum install supervisor
  • Debian/Ubuntu可通过apt安装
apt-get install supervisor
  • pip安装
pip install supervisor

3

Supervisor 监控shell脚本

使用Supervisor 来监控 shell 脚本的执行来给大家演示一下。

shell 脚本test.sh,每个一秒输出时间、环境变量PATH到 文件:


#!/bin/sh
while [ true ]; do
/bin/sleep 1
/bin/date >>date.txt
echo $PATH >>date.txt
done

注意:脚本权限 chmod +x test.sh

  • 配置Supervisor应用守护 通过运行echo_supervisord_conf程序生成supervisor的初始化配置文件,如下所示:
mkdir /etc/supervisor
echo_supervisord_conf > /etc/supervisor/supervisord.conf

修改 supervisord.conf 文件,在文件末尾添加

;conf.d 为配置表目录的文件夹,需要手动创建
[include]
files = conf.d/*.conf

其实和nginx 的 配置一样,方便将各个应用的配置文件隔离开来,然后把 conf.d目录下的所有.conf 文件都引进来。

  • 为应用编写守护配置文件,每个应用都应有个 .conf 文件,.conf文件位于/etc/supervisor/conf.d 目录下。
[program:test_supervisor] ;程序名称,终端控制时需要的标识,可以将文件命名为 test_supervisor.conf
command=sh test.sh ; 运行程序的命令
directory=/root/supervisor ; 命令执行的目录
autorestart=true ; 程序意外退出是否自动重启
stderr_logfile=/root/supervisor/test.err.log ; 错误日志文件
stdout_logfile=/root/supervisor/test.out.log ; 输出日志文件
user=root ; 进程执行的用户身份
stopsignal=INT
  • 启动Supervisor守护
[root@master supervisor]# supervisord -c /etc/supervisor/supervisord.conf
[root@master system]# ps -ef|grep test
root       3522   3437  0 08:25 ?        00:00:01 sh test.sh
root       8425   1670  0 08:44 pts/0    00:00:00 grep --color=auto test
[root@master system]# kill 3522
[root@master system]# ps -ef|grep test
root       8440   3437  0 08:44 ?        00:00:00 sh test.sh
root       8447   1670  0 08:44 pts/0    00:00:00 grep --color=auto test
[root@master system]# kill 8440
[root@master system]# ps -ef|grep test
root       8474   3437  0 08:44 ?        00:00:00 sh test.sh
root       8481   1670  0 08:45 pts/0    00:00:00 grep --color=auto test
[root@master system]#

可以看到,即使我没有执行 sh test.sh ,Supervisor也会给我们启动,而且当你杀掉一个test.sh 的进程时,Supervisor 又会出现新的进程。

4

Supervisor 开机自启动

以 centos 7 下设置Supervisor 开机自启动为例,其实其他应用也是使用这种方式设置开机自启动。

  • 新建一个.service 文件,supervisord.service,将文件拷贝至"/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
ExecStop=/usr/bin/supervisorctl shutdown
ExecReload=/usr/bin/supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

当你 使用systemctl 或者service 命令时 会用到这个.service 文件,相当于服务注册。

  • 执行命令
systemctl enable supervisord
  • 执行命令来验证是否为开机启动
[root@master system]# systemctl enable supervisord
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.
[root@master system]# systemctl is-enabled supervisord
enabled

5

Supervisor 常用命令

supervisorctl restart <application name> ;重启指定应用
supervisorctl stop <application name> ;停止指定应用
supervisorctl start <application name> ;启动指定应用
supervisorctl restart all ;重启所有应用
supervisorctl stop all ;停止所有应用
supervisorctl start all ;启动所有应用

重启单个应用,例如:

[root@master supervisor]# supervisorctl restart test_supervisor
test_supervisor: stopped
test_supervisor: started

此处 test_supervisor 是在/etc/supervisor/conf.d 目录下单个应用配置文件中
[program:test_supervisor]  指定的。

END