Ubuntu配置Supervisor管理进程

9 篇文章 0 订阅
1 篇文章 0 订阅

Supervisor的作用

Supervisor( http://supervisord.org )是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。它可以很方便的监听、启动、停止、重启一个或多个用户关注的进程。用Supervisor管理的进程,当进程意外被杀死,可以配置supervisort监听到进程死后,重新拉起。很方便地做到进程自动恢复,不再需要自己写shell脚本来控制。

supervisor组件

  1. supervisord(server 部分):主要负责管理子进程,响应客户端命令以及日志的输出等。
  2. supervisorctl(client 部分):命令行客户端,用户可以通过它与不同的 supervisord
    进程联系,获取子进程的状态等。
  3. Web Server界面管理进程:Web
    Server其实是通过XML_RPC来实现的,可以向supervisor请求数据,也可以控制supervisor及子进程。
  4. XML_RPC接口:提供了与webserver中相同的用于查询和控制进程的http接口。

安装

OS: Ubuntu 16.04
Kernel: 4.15.0
supervisor: 3.3.5 (默认版本)

sudo apt-get install supervisorl

supervisor安装完成后会生成三个执行程序:supervisortd、supervisorctl、echo_supervisord_conf,分别是supervisor的守护进程服务(用于接收进程管理命令)、客户端(用于和守护进程通信,发送管理进程的指令)、生成初始配置文件程序。
特别说明:由于用户权限问题,建议以root用户的权限执行这些程序。

>>>which supervisord                                                                                                                      
/home/bear/anaconda2/bin/supervisord
>>>which supervisorctl                                                                                                      
/home/bear/anaconda2/bin/supervisorctl
>>>which echo_supervisord_conf                                                                                             
/home/bear/anaconda2/bin/echo_supervisord_conf

配置

supervisor默认配置文件位置

>>>/etc/superisor/   tree
.
├── conf.d
│   └── redis.conf
└── supervisord.conf

可以看到supervisor主要包含两个配置部分,主程序配置文件和被管理程序配置文件集合。
如果没有制定的配置文件,可以手动通过echo_supervisord_conf程序手动生成。

sudo echo_supervisord_conf > /etc/supervisor/supervisord.conf

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

这里要注意 unix_http_server 跟 supervisorctl中的 sock 文件目录要一致 。从配置文件的include说明可以看出,程序启动以后会加载conf.d目录下所有的conf文件。

管理命令

可以使用以下命令启动、更新配置、更新进程、查看状态

#启动superisor主程序
>>> supervisord
# 读取有更新(增加)的配置文件,不会启动新添加的程序
>>> supervisorctl reread
# 重启配置文件修改过的程序
>>> supervisorctl update
# 查看程序状态
>>> supervisorctl status
# 启动程序 App_name
>>> supervisorctl start App_name
# 关闭程序 App_name
>>> supervisorctl stop App_name
# 重启程序 App_name
>>> supervisorctl restart App_name

启动主程序以后可以简单查看启动情况

>>>ps aux | grep suprvisor
root      4166  0.0  0.2  61780 19912 ?        Ss   3月09   0:05 /usr/bin/python /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf
bear     11796  0.0  0.0  15988  1012 pts/19   S+   14:32   0:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn supervisor

program配置

在conf.d目录下新建文件redis.conf

[program:redis-server]
#启动命令
command=redis-server
#自动启动
autostart=true
#自动重启
autorestart=true

查看目前os的端口占用情况

sudo netstat -tunlp                                                                                  
[sudo] bear 的密码: 
激活Internet连接 (仅服务器)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:9001          0.0.0.0:*               LISTEN      4166/python     
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      5169/redis-server 1
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1010/nginx -g daemo
tcp        0      0 127.0.0.1:5939          0.0.0.0:*               LISTEN      1047/teamviewerd
tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN      1323/dnsmasq    
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      14578/cupsd     
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      29176/python    
tcp        0      0 127.0.0.1:16835         0.0.0.0:*               LISTEN      3638/code       
tcp6       0      0 :::6379                 :::*                    LISTEN      23215/redis-server 
tcp6       0      0 :::80                   :::*                    LISTEN      1010/nginx -g daemo
tcp6       0      0 ::1:631                 :::*                    LISTEN      14578/cupsd     
udp        0      0 0.0.0.0:38718           0.0.0.0:*                           1323/dnsmasq    
udp        0      0 127.0.1.1:53            0.0.0.0:*                           1323/dnsmasq    
udp        0      0 0.0.0.0:68              0.0.0.0:*                           18269/dhclient  
udp        0      0 0.0.0.0:631             0.0.0.0:*                           14579/cups-browsed
udp        0      0 0.0.0.0:54016           0.0.0.0:*                           788/avahi-daemon: r
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           2198/chrome     
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           788/avahi-daemon: r
udp6       0      0 :::36993                :::*                                788/avahi-daemon: r
udp6       0      0 :::5353                 :::*                                2198/chrome     
udp6       0      0 :::5353                 :::*                                788/avahi-daemon: r

杀掉占用端口的进程

sudo kill -9 23215

将redis进程托管给Supervisor

supervisorctl reread
supervisorctl update

web管理界面

除了 supervisorctl 之外,还可以配置 supervisrod 启动 web 管理界面,这个 web 后台使用 Basic Auth 的方式进行身份认证。
编辑配置文件/etc/supervisor/supervisord.conf,添加inet_http_server节点

[inet_http_server]
port=127.0.0.1:9001
username=******
password=******

杀掉占用9001端口的进程以后重启supervisor程序。直接访问http://localhost:9001

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值