0%

Linux安装配置Supervisor

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
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

基于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

常用命令

1
2
3
4
5
6
7
8
9
10
11
# 重启supervisor
systemctl retart supervisord

# 重启被管理的服务
supervisorctl -c /etc/supervisord.conf restart service_name

# 查看被管理的服务状态
supervisorctl status

# 重新加载配置
supervisorctl restart

Supervisor实战

管理tomcat

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

管理Jupyter

参考《CentOS安装配置Jupyter》

管理Django

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

  • 本文作者: 好好学习的郝
  • 本文链接: https://www.voidking.com/dev-linux-supervisor/
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!源站会及时更新知识点及修正错误,阅读体验也更好。欢迎分享,欢迎收藏~