使用Supervisor管理Linux進程

使用Supervisor管理Linux進程

簡介

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

安裝

sudo pip install supervisor

 

配置

安裝完成 supervisor 以後,可使用 「echo_supervisord_conf」 命令來生成樣例配置文件linux

echo_supervisord_conf

 

默認 supervisor 會使用 /etc/supervisord.conf 做爲默認配置文件。web

啓動服務

服務程序

首先寫個小程序來模擬一個服務程序,以下 
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。bash

此時咱們使用 「ps -ef | grep myserver」 找到上面的服務進程,而後kill掉這個進程。此時就會看到日誌中 supervisor 會啓動一個新的myserver進程。socket

管理服務

對於上面的例子咱們只能啓動一個服務,卻不能管理這些配置的服務,下面就看看怎樣管理服務。ui

服務程序

仍是使用上面myserver.sh程序。url

配置

/etc/supervisord.confspa

[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

 

 

  • 運行客戶端,查看進程狀態,控制進程啓動或中止
supervisord : 啓動supervisor
supervisorctl reload :修改完配置文件後從新啓動supervisor
supervisorctl update:根據最新的配置文件,啓動新配置或有改動的進程,配置沒有改動的進程不會受影響而重啓
supervisorctl status :查看supervisor監管的進程狀態
supervisorctl start 進程名 :啓動XXX進程
supervisorctl stop 進程名 :中止XXX進程
supervisorctl stop all:中止所有進程,注:start、restart、stop都不會載入最新的配置文件
 

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> 

 

遠程管理

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

 

Web接口

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

轉載請以連接形式標明本文地址 


 

遇到的問題

 

解決方案:
首先你要讓文件有可以執行的權限,好比你的文件是a.sh那麼你能夠
chmod +x a.sh
而後運行文件就能夠了
./a.sh
這樣運行是a.sh在當前工做目錄,若是文件沒在當前目錄,那麼就須要用絕對路徑來執行,好比
/opt/a.sh
/opt/test/a.sh

解決方案:重啓進程
相關文章
相關標籤/搜索