进程管理工具(Supervisor)笔记

感谢GoFrame框架实战2群的 朽木自雕 大佬提供的Supervisor笔记

# 开发笔记

## [进程管理工具(Supervisor)](http://supervisord.org/introduction.html)

### 1、安装Python包管理工具([easy_install](https://pypi.org/project/setuptools/))

```
yum install python-setuptools
```
### 2、安装Supervisor

```
easy_install supervisor
```
### 3、配置Supervisor应用守护
a) 通过运行echo_supervisord_conf程序生成supervisor的初始化配置文件,如下所示:

```
mkdir /etc/supervisor
echo_supervisord_conf > /etc/supervisor/supervisord.conf
```
然后查看路径下的supervisord.conf。在文件尾部添加如下配置。

```
...

;[include]
;files = relative/directory/*.ini

;conf.d 为配置表目录的文件夹,需要手动创建
[include]
files = conf.d/*.conf
```
b) 为你的程序创建一个.conf文件,放在目录"/etc/supervisor/conf.d/"下。

```
[program:main-worker]
;process_name=%(program_name)s_%(process_num)02d
command= /www/wwwroot/test/main
autostart=true
autorestart=true
;user=forge
;numprocs=3
startsecs=1
startretries=10
redirect_stderr=true
stdout_logfile=/www/wwwroot/storage/logs/main.log
```
c) 运行supervisord,查看是否生效

```
supervisord -c /etc/supervisor/supervisord.conf
ps -ef | grep MGToastServer
```
结果:
![9709727-587293319af36520.png](https://cdn.nlark.com/yuque/0/2019/png/287471/1567059063956-dea1893a-e9b7-4a60-a7dd-37151d438a58.png#align=left&display=inline&height=70&name=9709727-587293319af36520.png&originHeight=70&originWidth=708&size=3850&status=done&width=708)
ps 如果服务已启动,修改配置文件可用“supervisorctl reload”命令来使其生效
### 4、配置Supervisor开机启动
a) 新建一个“supervisord.service”文件

```
# dservice for systemd (CentOS 7.0+)
# by ET-CS (https://github.com/ET-CS)
[Unit]
Description=Supervisor daemon

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

[Install]
WantedBy=multi-user.target
```
b) 将文件拷贝至"/usr/lib/systemd/system/supervisord.service"

c) 执行命令

```
systemctl enable supervisord
```
d) 执行命令来验证是否为开机启动

```
systemctl is-enabled supervisord
```
![11111.png](https://cdn.nlark.com/yuque/0/2019/png/287471/1567059187553-5491ec09-45e6-48f6-bfea-ab1b9f35860e.png#align=left&display=inline&height=92&name=11111.png&originHeight=92&originWidth=1063&size=6165&status=done&width=1063)
### 常用的相关管理命令

```
supervisorctl restart <application name> ;重启指定应用
supervisorctl stop <application name> ;停止指定应用
supervisorctl start <application name> ;启动指定应用
supervisorctl restart all ;重启所有应用
supervisorctl stop all ;停止所有应用
supervisorctl start all ;启动所有应用
```

猜你喜欢

转载自blog.csdn.net/lengyue1084/article/details/106478396