欢迎访问我的网站,希望内容对您有用,感兴趣的可以加入免费知识星球。

使用supervisor管理进程

Linux 迷途小书童 4年前 (2019-12-04) 7482次浏览 2个评论

软硬件环境

  • ubuntu 18.04 64bit
  • anaconda with python 3.6
  • supervisor 3.3.4

视频看这里

此处是youtube的播放链接,需要科学上网。喜欢我的视频,请记得订阅我的频道,打开旁边的小铃铛,点赞并分享,感谢您的支持。

supervisor简介

supervisor是一个用python语言编写的进程管理工具,它可以很方便的监听、启动、停止、重启一个或多个进程。当一个进程意外被杀死,supervisor监听到进程死后,可以很方便的让进程自动恢复,不再需要程序员或系统管理员自己编写代码来控制。

安装supervisor

当前版本的supervisor暂不支持python3,可喜的是基于python2supervisor同样可以管理使用python3编写的进程。我们使用下面命令安装

sudo apt-get install supervisor

仓库中默认也是只支持python2的,可以通过locate supervisord来验证下

xugaoxiang@ubuntu:~/Work/gogs/blog$ locate supervisord 
/usr/local/bin/echo_supervisord_conf
/usr/local/bin/supervisord
/usr/local/lib/python2.7/dist-packages/supervisor/supervisord.py
/usr/local/lib/python2.7/dist-packages/supervisor/supervisord.pyc
/usr/local/lib/python2.7/dist-packages/supervisor/tests/test_supervisord.py
/usr/local/lib/python2.7/dist-packages/supervisor/tests/test_supervisord.pyc

github上的supervisor代码仓库已经支持了python3,可以直接clone下来,通过sudo python3 setup.py install进行安装。

supervisor相关的几个命令

安装完毕,会生成3个系统命令supervisorctlsupervisordecho_supervisord_conf

  1. supervisord,运行supervisor时会启动一个进程supervisord,它负责启动所管理的进程,并将所管理的进程作为自己的子进程来启动,而且可以在所管理的进程出现崩溃时自动重启

  2. supervisorctl,是命令行管理工具,可以用来执行startstoprestart等命令,来对这些子进程进行管理, 如

    sudo supervisorctl start far

    其中far是进程的名称, 详细的命令及说明见下面的这张表

    命令 说明
    supervisorctl start program_name 启动某个进程
    supervisorctl stop program_name 停止某个进程
    supervisorctl restart program_name 重启某个进程
    supervisorctl status program_name 查看某个进程的状态
    supervisorctl stop all 停止全部进程
    supervisorctl reload 载入最新的配置文件,重启所有进程
    supervisorctl update 根据最新的配置,重启配置更改过的进程,未更新的进程不受影响
  3. echo_supervisord_conf用来生成默认的配置文件(默认配置文件,内容非常齐全且都有注释,适合用时查阅),用法是这样的

    echo_supervisord_conf > test.conf

supervisor的使用

这里以一个带参数的命令行工具为例,首先创建一个配置文件/etc/supervisor/conf.d/far.confconf.d目录下的所有进程配置文件都会通过/etc/supervisor/supervisord.conf被包含进来,为方便管理,我们都是将一个进程的管理写成一个配置文件,彼此一一对应。输入如下内容

xugaoxiang@ubuntu:/etc/supervisor/conf.d# cat far.conf 
[program:far]
command=FacialAttendanceRecord --config /home/xugaoxiang/www/config/sys.json --model /home/xugaoxiang/www/model/longjing.clf --dataset /home/xugaoxiang/www/storage --webport 8888 --threshold 0.38
directory=/home/xugaoxiang/www
autostart=true
autorestart=true
user=root
redirect_stderr=true

接下来启动服务

sudo /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf

这时候终端就会输出

2018-12-04 14:29:44,916 CRIT Supervisor is running as root.  Privileges were not dropped because no user is specified in the config file.  If you intend to run as root, you can set user=root in the config file to avoid this message.
2018-12-04 14:29:44,916 INFO Included extra file "/etc/supervisor/conf.d/far.conf" during parsing
2018-12-04 14:29:44,926 INFO RPC interface 'supervisor' initialized
2018-12-04 14:29:44,926 CRIT Server 'unix_http_server' running without any HTTP authentication checking
2018-12-04 14:29:44,926 INFO supervisord started with pid 32363
2018-12-04 14:29:45,931 INFO spawned: 'far' with pid 32366
2018-12-04 14:29:46,933 INFO success: far entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)

还可以通过supervisorctl status来查看进程的状态

xugaoxiang@ubuntu:~/www$ sudo supervisorctl status far
far                              RUNNING   pid 32366, uptime 0:01:11

如果在配置的过程出现错误,在系统进程表中没有自己预期要运行的进程,那就需要自己去找原因了。一般的解决思路有

  1. 通过在命令行中运行来检查进程本身是否可以正常运行?
  2. 查看supervisor日志文件,如/var/log/supervisor/supervisord.log
  3. 通过supervisorctl工具调试
  4. ubuntu系统中注意权限问题

supervisor管理进程的多个实例

在原来配置的基础之上,再加入进程数目以及相应进程的名字,如下

process_name=%(program_name)s_%(process_num)02d
numprocs=2

所以上面的far.conf就变成了下面的样子

xugaoxiang@ubuntu:/etc/supervisor/conf.d# cat far.conf 
[program:far]
command=FacialAttendanceRecord --config /home/xugaoxiang/www/config/sys.json --model /home/xugaoxiang/www/model/longjing.clf --dataset /home/xugaoxiang/www/storage --webport 8888 --threshold 0.38
directory=/home/xugaoxiang/www
process_name=%(program_name)s_%(process_num)02d
numprocs=2
autostart=true
autorestart=true
user=root
redirect_stderr=true

supervisor的web化

有些童鞋生来讨厌命令行(那还学什么linux?),庆幸的是supervisor提供了基于web的控制,管理员可以通过在页面上点点按钮即可完成对进程的启动、重启等操作,甚是方便。

首先需要编辑/etc/supervisor/supervisor.conf, 添加语句

[inet_http_server]
port=127.0.0.1:9001
;username=xugaoxiang
;password=xugaoxiang

然后重启服务

sudo /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf

打开浏览器,输入地址127.0.0.1:9001

web_supervisor

设置环境变量

有时候需要在进程执行之前设置环境变量,可以编辑/etc/supervisor/conf.d/*.conf

environment =
    SQLALCHEMY_DATABASE_URI="mysql+pymysql://root:toor@localhost/dcenter",
    FLASK_DEBUG=True

多个环境变量用逗号隔开即可

参考资料

喜欢 (3)

您必须 登录 才能发表评论!

(2)个小伙伴在吐槽
  1. 使用不了,系统linux版本为Linux 3.10.0-1062.1.2.el7.x86_64
    匿名2021-07-11 22:02