supervisor安装与使用

supervisor是python开发的linux进程管理工具,可以很方便的控制进程。

其主要的组件:

  • supervisord

是服务端程序,主要功能是启动supervisord服务,启动supervisor管理的子进程,对进程进行管理的服务。

  • supervisorctl

是客户端程序,主要功能就是管理(启动/关闭/重启/状态等)子进程,提供shell环境进行处理。

  • web server

Web Server主要可以在界面上管理进程,Web Server其实是通过XML_RPC来实现的,可以向supervisor请求数据,也可以控制supervisor及子进程。配置在[inet_http_server]块里面

  • XML_RPC

远程调用的,上面的supervisorctl和Web Server就它实现

1. 安装

1.1. 在线安装

1
easy_install supervisor

或者

1
pip install supervisor

1.2. 离线安装

如果需要安装supervisor的机器没有Internet访问权限,需要提前下载好安装包,手动copy到机器上,解压后执行python setup.py install

需要的安装包

  • setuptools
  • supervisor

下载地址:

https://pypi.org/project/setuptools/#files
https://pypi.org/project/supervisor/#files

note: 最新的(45.0.0及以后)setuptools需要python3

依次解压安装:

script
1
2
3
unzip setuptools-44.1.1.zip 
cd setuptools-44.1.1/
python setup.py install
script
1
2
3
tar -zxvf supervisor-4.2.0.tar.gz
cd supervisor-4.2.0/
python setup.py install

2. 测试安装是否成功

1
echo_supervisord_conf

会将一个”sample” Supervisor configuration输出到控制台。

3. 生成配置文件

1
echo_supervisord_conf > /etc/supervisord.conf

Supervisor配置文件通常命名为supervisord.conf。 它由supervisord和supervisorctl使用。 如果任一应用程序在没有-c选项的情况下启动(用于显式指示配置文件名的选项),应用程序将按照指定的顺序在以下位置查找名为supervisord.conf的文件。 它将使用它找到的第一个文件。

  1. $CWD/supervisord.conf
  2. $CWD/etc/supervisord.conf
  3. /etc/supervisord.conf
  4. /etc/supervisor/supervisord.conf (since Supervisor 3.3.0)
  5. ../etc/supervisord.conf (Relative to the executable)
  6. ../supervisord.conf (Relative to the executable)

$CWD表示运行supervisord程序的目录。

4. 配置进程

创建目录:

1
mkdir -m 755 /etc/supervisor.d

编辑/etc/supervisord.conf将最后的include注释打开。

1
2
[include]
files=/etc/supervisor.d//*.conf

/etc/supervisor.d/目录下创建进程配置hexo.conf

script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#项目名
[program:hexo]
#脚本目录
directory=/opt/hexo
#脚本执行命令
command=hexo server

#脚本运行的用户身份
user=root
#supervisor启动的时候是否随着同时启动,默认True
autostart=true
autorestart=true
stopasgroup=true
startsecs=10
#失败重试次数,默认3
startretries=999
stdout_logfile=/var/log/hexo/hexo.log
##日志文件大小,默认50M
stdout_logfile_maxbytes=20MB
#stdout日志文件备份数,默认10
stdout_logfile_backups=20
stderr_logfile=/var/log/hexo/hexo_err.log
stderr_logfile_maxbytes=10MB
#stderr日志文件备份数
stderr_logfile_backups=20

5. 启动

  • 启动supervisord

    1
    supervisord -c /etc/supervisord.conf
  • 启动supervisorctl

    1
    supervisorctl -c /etc/supervisord.conf

6. 使用

supervisord启动成功后,在终端输入supervisorctl管理配置的进程。

常用命令

1
2
3
4
5
6
7
8
9
status --查看进程状态
start [进程名] --启动进程
stop [进程名] --关闭进程
restart [进程名] --重启进程
restart all --重启所有应用
stop all --停止所有应用
start all --启动所有应用
update --更新新的配置到supervisord
reload --重新启动配置中的所有程序

7. web UI

打开注释

script
1
2
3
4
[inet_http_server]         ; inet (TCP) server disabled by default
port=*:9001 ; ip_address:port specifier, *:port for all iface
username=user ; default is no username (open server)
password=123 ; default is no password (open server)

启动后访问 ip:9001即可。

8. 开机自启动

新建文件/usr/lib/systemd/system/supervisord.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Unit]
Description=Supervisor daemon

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
ExecStop=/usr/bin/supervisorctl shutdown
ExecReload=/usr/bin/supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

开机自启动

1
systemctl enable supervisord

查看服务状态:

1
systemctl status supervisord

验证一下是否为开机启动

1
systemctl is-enabled supervisord

更多信息请访问官网:http://supervisord.org


supervisor安装与使用
https://www.wekri.com/linux/supervisor/
Author
Echo
Posted on
May 18, 2018
Licensed under