一个计算机技术爱好者与学习者

0%

好好学Linux:Linux中安装配置Supervisor

1. 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就可以管理它对应的进程。

相关文档:

2. 安装Supervisor

2.1. 基于CentOS7

1、安装supervisor

1
2
3
4
5
6
7
# 方法一
yum install epel-release
yum install supervisor

# 方法二
yum install python-pip
pip install supervisor

2、准备配置文件

1
2
3
mkdir -p /etc/supervisor/
echo_supervisord_conf > /etc/supervisord.conf
vim /etc/supervisord.conf

修改配置文件,添加:

1
2
3
[include]
files = /etc/supervisor/*.ini
;files = /etc/supervisor/*.conf

3、启动服务&设置开机启动

1
2
systemctl start supervisord
systemctl enable supervisord

2.2. 基于pyenv

如果系统的Python版本不适合Supservisor运行,那么可以使用pyenv切换Python环境安装Supervisor。
参考《Python版本管理器pyenv》,安装好python2.7.13。

1、新建supervisor环境

1
pyenv virtualenv 2.7.13 supervisor

2、激活supervisor环境

1
2
3
source /root/.pyenv/versions/2.7.13/envs/supervisor/bin/activate supervisor
# or
source activate supervisor

3、安装supervisor

1
pip install supervisor

4、准备配置文件

1
2
3
mkdir -p /etc/supervisor/
echo_supervisord_conf > /etc/supervisord.conf
vim /etc/supervisord.conf

修改配置文件,添加:

1
2
3
[include]
files = /etc/supervisor/*.ini
;files = /etc/supervisor/*.conf

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
2
3
4
5
6
7
8
9
10
11
12
[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service nss-user-lookup.target

[Service]
Type=forking
ExecStart=/root/.pyenv/versions/2.7.13/envs/supervisor/bin/supervisord -c /etc/supervisord.conf
ExecReload=/root/.pyenv/versions/2.7.13/envs/supervisor/bin/supervisorctl reload
ExecStop=/root/.pyenv/versions/2.7.13/envs/supervisor/bin/supervisorctl shutdown

[Install]
WantedBy=multi-user.target

7、重新启动服务&设置开机启动

1
2
3
4
ps aux | grep supervisord
systemctl stop supervisord
systemctl start supervisord
systemctl enable supervisord

3. 常用命令

重启supervisor

1
systemctl retart supervisord

查看被管理的服务状态

1
supervisorctl status

读取有更新或增加的配置文件,但并不加载。

1
supervisorctl reread

重新加载配置,并根据新的配置重新启动、停止进程

1
supervisorctl reload

停止一个服务

1
supervisorctl stop service_name

启动一个服务

1
supervisorctl start service_name

重启一个服务

1
supervisorctl restart service_name

4. Supervisor实战

4.1. 管理tomcat

参考《CentOS7设置tomcat开机自启动》

4.2. 管理Jupyter

参考《CentOS安装配置Jupyter》

4.3. 管理Django

参考Django部署到线上(修改版)

  • 本文作者: 好好学习的郝
  • 原文链接: https://www.voidking.com/dev-linux-supervisor/
  • 版权声明: 本文采用 BY-NC-SA 许可协议,转载请注明出处!源站会即时更新知识点并修正错误,欢迎访问~
  • 微信公众号同步更新,欢迎关注~