歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> 使用Supervisor管理Linux進程

使用Supervisor管理Linux進程

日期:2017/3/1 12:18:51   编辑:關於Linux

使用Supervisor管理Linux進程

簡介

Supervisor是一個C/S系統,它可以在類UNIX系統上控制系統進程,由python編寫,提供了大量的功能來實現對進程的管理。

安裝

sudo pip install supervisor

配置

安裝完成 supervisor 之後,可以使用 “echo_supervisord_conf” 命令來生成樣例配置文件

echo_supervisord_conf

默認 supervisor 會使用 /etc/supervisord.conf 作為默認配置文件。

啟動服務

服務程序

首先寫個小程序來模擬一個服務程序,如下
myserver.sh

#!/bin/sh

while true
do
    date 
    sleep 5
done

配置

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

[supervisord]
nodaemon=true

[program:myserver]
command=/home/kongxx/test/myserver.sh

啟動服務

supervisord -c /etc/supervisord.conf

運行上面的程序即可啟動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

啟動服務

supervisord -c /etc/supervisord.conf

查詢/啟動/停止服務

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

$ supervisorctl start myserver
$ supervisorctl stop myserver

supervisor 管理命令行

supervisorctl也可以不帶任何參數,此時即可進入supervisor的管理命令行接口,如下:

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

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

mysupervisor> 

遠程管理

supervisorctl -s http://:9999 -u admin -p Letmein status myserver

Web接口

可以使用浏覽器訪問 http://:9999 來通過web接口管理服務。

Copyright © Linux教程網 All Rights Reserved