Linux進程管理工具——supervisor

介紹

Supervisord是用Python實現的一款非常實用的進程管理工具


安裝

這裏用源碼

supervisor-3.1.3.tar.gz
tar -zxvf supervisor-3.1.3.tar.gz
cd supervisor-3.1.3
sudo python setup.py install

生成默認配置文件

echo_supervisord_conf > /etc/supervisord.conf  

修改默認配置文件

如需允許web界面和命令行訪問,修改inet_http_server配置

[inet_http_server]  
port=*:9001     ;這裏*表示可以讓其他終端訪問supervisor web界面
username=username ;  用戶名  
password=password  ;  密碼  

未知  具體功能,需修復

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket

修改進程

複製代碼
[program:test_http]
command=python test_http.py 501  ; 被監控的進程路徑
directory=/home/admin/soft/supervisor-3.1.3                ; 執行前要不要先cd到目錄去,一般不用
priority=1                    ;數字越高,優先級越高
numprocs=1                    ; 啓動幾個進程
autostart=true                ; 隨着supervisord的啓動而啓動
autorestart=true              ; 自動重啓。。當然要選上了
startretries=10               ; 啓動失敗時的最多重試次數
exitcodes=0                   ; 正常退出代碼(是說退出代碼是這個時就不再重啓了嗎?待確定)
stopsignal=KILL               ; 用來殺死進程的信號
stopwaitsecs=10               ; 發送SIGKILL前的等待時間
redirect_stderr=true          ; 重定向stderr到stdout
stdout_logfile=/home/jihite/log/supervisor.log
複製代碼

這裏test_http.py 位於directory(/home/admin/soft/supervisor-3.1.3)目錄下

test_http.py

複製代碼
import sys  
import BaseHTTPServer  
from SimpleHTTPServer import SimpleHTTPRequestHandler  
HandlerClass = SimpleHTTPRequestHandler  
ServerClass = BaseHTTPServer.HTTPServer  
Protocol = "HTTP/1.0"  
  
if __name__ == "__main__":
    if sys.argv[1:]:  
        port = int(sys.argv[1])  
    else:  
        port = 8000  

    server_address = ('10.125.24.105', port)  
    HandlerClass.protocol_version = Protocol  
    httpd = ServerClass(server_address, HandlerClass)  
    
    sa = httpd.socket.getsockname()  
    print "Serving HTTP on", sa[0], "port", sa[1], "..."  
    httpd.serve_forever() 
複製代碼

啓動

sudo  supervisord -c /etc/supervisord.conf

管理

命令管理

複製代碼
sudo supervisorctl
status: 查看當前運行的進程列表
stop xxx: 停止某一個進程(xxx),xxx爲[program:theprogramname]裏配置的值。
start xxx: 啓動某個進程
restart xxx: 重啓某個進程
stop groupworker: 重啓所有屬於名爲groupworker這個分組的進程(start,restart同理)
stop all,停止全部進程,注:start、restart、stop都不會載入最新的配置文件。
複製代碼

重新加載配置文件

sudo  supervisorctl -c /etc/supervisord.conf

通常使用時

supervisorctl status #查看當前進程狀態
supervisorctl start/restart...	xxx	#執行操作

界面管理


異常

1. Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.

For help, use /home/admin/idcp-check/virtualenv/bin/supervisord -h

解決:(參考)

sudo unlink /tmp/supervisor.sock
or
sudo unlink /var/run/supervisor.sock

2. 啓動不起來

$sudo supervisorctl 
create_roadnet_tasks3 FATAL Exited too quickly (process log may have details)
解決:(參考

Check if supervisor startsecs=0   #被監控程序啓動時持續時間

3. 啓動不起來,日誌提示不能用root用戶啓動

Running a worker with superuser privileges when the
worker accepts messages serialized with pickle is a very bad idea!

If you really want to continue then you have to set the C_FORCE_ROOT
environment variable (but please think about this before you do).

解決

複製代碼
from celery import Celery, platforms
app = Celery('tasks', broker='amqp://myuser:
[email protected] :5672/vhost')
platforms.C_FORCE_ROOT = True        #加上這一行

@app.task def add(x, y): return x + y
複製代碼