Supervisor简介
Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.
It shares some of the same goals of programs like launchd, daemontools, and runit. Unlike some of these programs, it is not meant to be run as a substitute for init as “process id 1”. Instead it is meant to be used to control processes related to a project or a customer, and is meant to start like any other program at boot time.
Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。它是通过fork/exec的方式把这些被管理的进程当作supervisor的子进程来启动,这样只要在supervisor的配置文件中,把要管理的进程的可执行文件的路径写进去即可。也实现当子进程挂掉的时候,父进程可以准确获取子进程挂掉的信息,可以选择是否自己启动和报警。supervisor还提供了一个功能,可以为supervisord或者每个子进程,设置一个非root的user,这个user就可以管理它对应的进程。
相关文档:
安装Supervisor
基于CentOS7
1、安装supervisor
1 | # 方法一 |
2、准备配置文件
1 | mkdir -p /etc/supervisor/ |
修改配置文件,添加:
1 | [include] |
3、启动服务&设置开机启动
1 | systemctl start supervisord |
基于pyenv
如果系统的Python版本不适合Supservisor运行,那么可以使用pyenv切换Python环境安装Supervisor。
参考《Python版本管理器pyenv》,安装好python2.7.13。
1、新建supervisor环境
1 | pyenv virtualenv 2.7.13 supervisor |
2、激活supervisor环境
1 | source /root/.pyenv/versions/2.7.13/envs/supervisor/bin/activate supervisor |
3、安装supervisor
1 | pip install supervisor |
4、准备配置文件
1 | mkdir -p /etc/supervisor/ |
修改配置文件,添加:
1 | [include] |
5、测试运行
1 | /root/.pyenv/versions/2.7.13/envs/supervisor/bin/supervisord -c /etc/supervisord.conf |
6、配置supervisord.service
1 | vi /usr/lib/systemd/system/supervisord.service |
修改为:
1 | [Unit] |
7、重新启动服务&设置开机启动
1 | ps aux | grep supervisord |
常用命令
1 | # 重启supervisor |