Supervisor是用Python编写的

目前只能在类unix操作系统中使用的一个进程管理工具

官网 http://supervisord.org/

安装

centos上安装

yum install supervisor

Ubuntu上安装

apt-get install supervisor

pip安装

pip install supervisor

当前系统Ubuntu16.04.7

安装完成后

查看版本

$ supervisord --version
3.2.0

supervisor有两种命令类型:

  supervisord 是服务相关的命令

  supervisorctl 是客户端相关的命令

配置文件再/etc/supervisor目录下supervisord.conf

; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

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

每个进程的配置文件可以放在 /etc/supervisor/conf.d 下

eg:suTest.py

import time
def test():
    while True:
        print "test"
        time.sleep(10)
test()

在 /etc/supervisor/conf.d 下添加 suTest.conf

[program:suTest]
command= python suTest.py
directory=/home/baby
user=root
autostart=true
autorestart=true

说明:

  autostart=true     是否随supervisord启动而启动

  autorestart=true  进程意外退出后是否自动重启

配置好后执行

$ sudo supervisorctl reload
Restarted supervisord

查看supervisorctl状态

$ sudo supervisorctl status
suTest                           RUNNING   pid 3879, uptime 0:00:23

关闭子进程suTest

$ sudo supervisorctl stop suTest
suTest: stopped
$ sudo supervisorctl status
suTest                           STOPPED   Dec 18 04:59 PM

启动子进程suTest

$ sudo supervisorctl start suTest
suTest: started
$ sudo supervisorctl status
suTest                           RUNNING   pid 3889, uptime 0:00:06

重启子进程suTest

$ sudo supervisorctl restart suTest
suTest: stopped
suTest: started
$ sudo supervisorctl status
suTest                           RUNNING   pid 3894, uptime 0:00:03

杀死进程 3894 后,自动重启,即产生了一个新的进程

$ sudo kill -9 3894
$ sudo supervisorctl status
suTest                           RUNNING   pid 3899, uptime 0:00:02

supervisord就可以起到守护进程的作用了

如果遇到因权限而失败的问题

$ sudo chmod 777 /var/run
$ sudo chmod 777 /etc/supervisor

启用web管理页面进行进程可视化进程管理

开启[inet_http_server],即可通过web界面进行管理

修改/etc/supervisor/supervisord.conf

[inet_http_server]
port=172.2.0.228:9001
username=user
password=123456

重新加载配置

sudo supervisorctl reload

或更新配置

sudo supervisorctl update

http://172.2.0.228:9001/

进程管理工具 Supervisor_子进程

 查看所有启动过的supervisord服务

$ ps -fe | grep supervisord
root      4109     1  0 17:19 ?        00:00:00 /usr/bin/python /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf
baby      4139  2079  0 17:31 pts/0    00:00:00 grep --color=auto supervisord