使用Supervisor管理Linux进程

简介

yum search supervisor
yum install supervisor


Supervisor是一个C/S系统,它可以在类UNIX系统上控制系统进程,由Python编写,提供了大量的功能来实现对进程的管理。

安装


  1. # yum install python-setuptools
  2. # easy_install supervisor
  3. 如果easy_install不好使就从官方下载:


或者
sudo pip install supervisor
 
 
  • 1
  • 1

配置

安装完成 supervisor 之后,可以使用 “echo_supervisord_conf” 命令来生成样例配置文件

echo_supervisord_conf
 
 
  • 1
  • 1

默认 supervisor 会使用 /etc/supervisord.conf 作为默认配置文件。

启动服务

服务程序

首先写个小程序来模拟一个服务程序,如下 
myserver.sh

#!/bin/sh

while true
do
    date 
    sleep 5
done
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

配置

修改配置文件 /etc/supervisord.conf ,内容如下

[supervisord]
nodaemon=true

[program:myserver]
command=/home/kongxx/test/myserver.sh
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

启动服务

supervisord -c /etc/supervisord.conf
 
 
  • 1
  • 1

运行上面的程序即可启动supervisor服务,此时会在当前目录下生成一个日志文件 supervisord.log。

此时我们使用 “ps -ef | grep myserver” 找到上面的服务进程,然后kill掉这个进程。此时就会看到日志中 supervisor 会启动一个新的myserver进程。

管理服务

对于上面的例子我们只能启动一个服务,却不能管理这些配置的服务,下面就看看怎样管理服务。

服务程序

还是使用上面myserver.sh程序。

配置

/etc/supervisord.conf

[inet_http_server]         ; inet (TCP) server disabled by default
port = *:9999              ; (ip_address:port specifier, *:port for all iface)
username = admin           ; (default is no username (open server))
password = Letmein         ; (default is no password (open server))

[supervisord]
nodaemon = false

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl = http://127.0.0.1:9999 ; use an http:// url to specify an inet socket
username = admin              ; should be same as http_username if set
password = Letmein            ; should be same as http_password if set
prompt = mysupervisor         ; cmd line prompt (default "supervisor")

[program:myserver]
command = /home/kongxx/test/myserver.sh
redirect_stderr = true
stdout_logfile = /tmp/myserver.log

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

启动服务

supervisord -c /etc/supervisord.conf
 
 
  • 1
  • 1

查询/启动/停止服务

$ supervisorctl status myserver
myserver                         RUNNING   pid 14034, uptime 0:00:03 

$ supervisorctl start myserver
$ supervisorctl stop myserver
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

supervisor 管理命令行

supervisorctl也可以不带任何参数,此时即可进入supervisor的管理命令行接口,如下:

$ supervisorctl 
myserver                         RUNNING   pid 15297, uptime 0:00:27
mysupervisor> ?

default commands (type help <topic>):
=====================================
add    exit      open  reload  restart   start   tail   
avail  fg        pid   remove  shutdown  status  update 
clear  maintail  quit  reread  signal    stop    version

mysupervisor> 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

远程管理

supervisorctl -s http://<ip>:9999 -u admin -p Letmein status myserver
 
 
  • 1
  • 1

Web接口

可以使用浏览器访问 http://:9999 来通过web接口管理服务。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值