1.平台版本和python环境

#系统版本7.3
[root@server]$ cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 

#supervisor依赖于python
[root@server]$ python -V
Python 2.7.5
#安装pip,这一步主要是为通过pip来安装supervisor而用。
yum install python-pip

2.安装supervisor(两种方式)

在扩展epel源安装好的情况下:

yum install supervisor
软件包 supervisor-3.1.4-1.el7.noarch 已安装并且是最新版本
#安装成功

或者通过pip 安装

[root@server]$ pip install supervisor
Installing collected packages: supervisor
Successfully installed supervisor-4.2.0
You are using pip version 8.1.2, however version 20.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
#安装成功
#pip升级
 pip install --upgrade pip

3.配置文件

通过yum安装的配置文件路径:

/etc/supervisord.conf        # supervisord配置文件
/etc/supervisord.d/          # 任务配置文件目录,需要管理的后台任务得放在这个文件夹下,且文件后缀得以ini结尾
# 可以去 /etc/supervisord.conf  配置最后一行修改读取配置文件的后缀

4.服务启动

systemctl start supervisord
systemctl enable supervisord

5.运用

准备一个要在后台运行的脚本

#脚本路径
[root@server ~]$ ls -l /opt/test/test.sh 
-rwxr-xr-x 1 root 128 7月  21 18:05 /opt/test/test.sh
#脚本内容
[root@server ~]$ cat !$
cat /opt/test/test.sh
#!/bin/bash
count = 1
while true
do  
  echo "----------supervisor TEST------------"
  sleep 2
  echo $count
  let count++
done

/etc/supervisord.d/test.ini文件配置

[root@server ~]$ ls -l /etc/supervisord.d/test.ini 
-rw-r--r-- 1 root 178 7月  21 18:06 /etc/supervisord.d/test.ini
#配置文件内容
[root@server ~]$ cat !$
cat /etc/supervisord.d/test.ini
[program:test1]
directory=/opt/test
command=/opt/test/test.sh
autostart=true
autorestart=true
stderr_logfile=/tmp/test_stderr.log
stdout_logfile=/tmp/test_stdout.log
user = root

6.进程维护

[root@server ~]$ supervisorctl status
test1                            RUNNING   pid 50897, uptime 0:15:50
#进入supervisor可以stop、resstart任务
[root@server ~]$ supervisorctl 
test1                            RUNNING   pid 50897, uptime 0:17:10
supervisor> 
supervisor> ?
default commands (type help <topic>):
=====================================
add    clear  fg        open  quit    remove  restart   start   stop  update 
avail  exit   maintail  pid   reload  reread  shutdown  status  tail  version

supervisor> restart test1
test1: stopped
test1: started
supervisor> 

注意:经本人手动测试,kill 掉test1服务的进程,可以被supervisord自动重启。

[root@server ~]$ kill 54867
#supervisor运行新的进程
supervisor> status
test1                            RUNNING   pid 54867, uptime 0:04:05
supervisor> status
test1                            RUNNING   pid 55818, uptime 0:00:06
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐