Linux:supervisor命令的使用

supervisor是Linux下一個便利的啓動和監控服務的命令。html

舉例來講:假如我想同時管理一堆的服務,包括他們的運行和中止。我就可使用supervisor來管理。
 
supervisor包括兩個命令:supervisord和supervisorctl,分別是後臺的守護進程以及命令行管理命令。要安裝這兩個命令只須要執行sudo apt-get install supervisor便可。
兩個命令共用一個配置文件,默認是:/etc/supervisor/supervisor.conf,而supervisor.conf 經過[include] 這個section來引入其它配置文件,通常放在/etc/supervisor/conf.d目錄,因此咱們能夠將須要的文件放到這個目錄之下,並將後綴改成.conf。
 
除了默認的配置以外,一個經常使用的配置方式是在使用啓動的時候指定特定的配置文件,配置方式爲:  sudo  supervisord  -c  /path/to/supervisor.conf    ,注意,這時候的配置文件要使用絕對路徑,而且配置文件要完整,不然可能會報錯。
supervisord只會啓動一個supervisord守護進程,剩下的管理工做都交給supervisorctl命令來完成。一樣的,若是指定了某一個配置文件來啓動服務,那麼supervisorctl的命令也要加上相對應的路徑來管理:sudo supervisorctl -c /path/to/supervisor.conf,這個命令敲下以後就會進入命令行模式,對應用進行管理。
 

典型的supervisor.conf配置以下:(配置文件的註釋用分號開頭)

; supervisor config file
[unix_http_server]  ; supervisor與supervisorctl的通信
file=/var/run/supervisor.sock   ; (the path to the socket file)  should match serverurl in section supervisorctl
chmod=0700                       ; sockef file mode (default 0700)
 
[supervisord]  ; 必須有,用來配置supervisord
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)
 
; the below section must remain in the config file for RPC  
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor] ; 這個配置項必須有
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
 
[supervisorctl]   ; 必須有,用於配置supervisorctl
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket
 
; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.
 
[include]  ;引入其它配置文件
files = /path/to/*.conf  # other configuration files you want to include
 
[program:api4c]  ;應用程序的配置 
command=/path/to/python/env/bin/gunicorn -w 1 -b 0.0.0.0:8888  wsgi:application ; 執行你的命令,不能是用於啓動守護進程,由於supervisor就是用來做爲守護進程的
directory=/home/ubuntu/4c-entry/api4c  ; chdir to here before command run
startsecs=0        ;
stopwaitsecs=0     ;
autostart=true    ; supervisord啓動的時候自動運行
autorestart=true  ; 當程序中斷的時候是否重啓 stop,true,false,unexpected
stdout_logfile=/path/to/logfile.log  ; 必須已經存在
stderr_logfile=/path/to/std/err/log/for4c.err

 

上面是一個基本的服務,用於管理一個服務進程,包含幾個必要的section:supervisord、supervisorctl、program:your_program_name、rpcinterface:supervisor、include。
這幾個是必定要配置的,缺乏supervisord和supervisorctl和rpcinterface:supervisor會報錯,include是可選的,若是你沒有引入其餘的配置文件的話。
關於supervisord和supervisorctl的通信方式有兩種:一種是經過sock來傳輸,也就是unix_http_server的配置,另外一種是inet_http_server,經過http進行通信,詳細的設置能夠參考supervisor的配置文檔。
 

supervisor的經常使用命令:

supervisord -c supervisor.conf                             經過配置文件啓動supervisor
supervisorctl -c supervisor.conf status                    察看supervisor的狀態
supervisorctl -c supervisor.conf reload                    從新載入 配置文件  更新後能夠選擇從新載入
supervisorctl -c supervisor.conf start [all] |  [appname]     啓動指定/全部 supervisor管理的程序進程
supervisorctl -c supervisor.conf stop [all] | [appname] 

 

命令使用實例:python

來源: http://chenxiaoyu.org/2011/05/31/python-supervisor.html  web

配置項:shell

[program:hello] 
command=python /home/smallfish/hello.py 
autorstart=true 
stdout_logfile=/home/smallfish/hello.log
命令
shell> sudo /etc/init.d/supervisor start   -- 啓動supervisor服務 
shell> sudo supervisorctl status hello     -- 獲取hello服務的狀態,由於是autorstart,這裏已經啓動了 hello  RUNNING    pid 1159, uptime 0:20:32 
shell> sudo supervisorctl stop hello       -- 中止hello服務 hello: stopped 
shell> sudo supervisorctl stop hello       -- 再次中止hello,會有錯誤信息 hello: ERROR (not running) 
shell> sudo supervisorctl start hello      -- 啓動hello服務 hello: started

 

經常使用的配置項:

詳見: http://supervisord.org/configuration.htmlubuntu

相關文章
相關標籤/搜索