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

界面管理

異常

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))

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM