Linux進程管理工具——supervisor

介紹

Supervisord是用Python實現的一款很是實用的進程管理工具python

安裝

這裏用源碼laravel

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配置web

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

未知  具體功能,需修復app

[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

修改進程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)目錄下ide

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

管理

命令管理ui

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

從新加載配置文件this

sudo  supervisorctl -c /etc/supervisord.conf

界面管理url

異常

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:
mypassword@localhost :5672/vhost')
platforms.C_FORCE_ROOT = True        #加上這一行

@app.task def add(x, y): return x + y

4. 啓動後,用supervisorctl 查看是出現異常:

http://127.0.0.1:9001 refused connection

解決,去掉註釋

[inet_http_server]         ; inet (TCP) server disabled by default
port=127.0.0.1:9001        ; (ip_address:port specifier, *:port for all iface)
#username=user              ; (default is no username (open server))
#password=123               ; (default is no password (open server))
相關文章
相關標籤/搜索