一、什麼是supervisor
Supervisor是一種針對UNIX系統的客戶端/伺服器系統,它可以用來使運行在同一台物理機上的多個進程同時工作。使用Supervisor,你可以簡單地監控和控制這些進程,以達到保持它們的狀態與持續工作的效果。
二、安裝supervisor
下面是安裝supervisor的步驟。
1. 安裝pip
執行以下命令安裝pip:
sudo apt-get install python-pip
2. 安裝supervisor
使用pip命令安裝Supervisor:
sudo pip install supervisor
3. 配置supervisor
一旦安裝了Supervisor,我們就需要進行一些配置。
首先,在終端中執行一下命令創建一個默認配置文件:
echo_supervisord_conf >> /etc/supervisord.conf
保存之後,我們可以打開配置文件並對其進行修改:
sudo vi /etc/supervisord.conf
注意:使用vim或者nano也可以進行編輯。
在配置文件中可以看到如下所示的內容:
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Note: shell expansion ("~" or "$HOME") is not supported. Environment
; variables can be expanded using this syntax: "%(ENV_VAR_NAME)s".
;
[unix_http_server]
file=/tmp/supervisor.sock ; (the path to the socket file)
[supervisord]
logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/tmp/ ; ('AUTO' child log dir, default $TEMP)
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
[program:foo]
command=/bin/cat ; the program (relative uses PATH, can take args)
process_name=%(program_name)s ; process_name expr (default %(program_name)s)
numprocs=1 ; number of processes copies to start (def 1)
directory=/tmp ; directory to cwd to before exec (def no cwd)
autostart=true ; start at supervisord start (default: true)
startsecs=1 ; # of secs prog must stay up to be running (def. 1)
startretries=3 ; max # of serial start failures when starting (default 3)
autorestart=unexpected ; autorestart if exited after running (def: unexpected)
exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
stopsignal=QUIT ; signal used to kill process (default TERM)
stopwaitsecs=10 ; max num secs to wait before SIGKILL (default 10)
stopasgroup=false ; send stop signal to the UNIX process group (default false)
killasgroup=false ; SIGKILL the UNIX process group (def false)
這是一個示例配置文件。我們可以根據自己的需求修改或者添加配置。
4. 啟動supervisor
執行以下命令以啟動supervisor:
sudo supervisord -c /etc/supervisord.conf
注意:如果沒加-c參數指定supervisord.conf文件路徑,則supervisor會默認使用/etc/supervisord.conf文件路徑。
5. 監控supervisor
執行以下命令可以查看supervisor是否在運行。
sudo supervisorctl status
如果你看到類似以下的輸出:
unix:///tmp/supervisor.sock:ERROR (no such file)
這可能是因為supervisord服務已經停止了。執行以下命令可以啟動服務:
sudo service supervisor start
三、使用supervisor
下面通過一個示例說明如何使用supervisor。
首先,創建一個簡單的Python腳本:
#!/usr/bin/env python
import time
while True:
print "Hello World!"
time.sleep(1)
保存為hello.py。然後在supervisord.conf中添加如下內容:
[program:hello]
command=/usr/bin/python /path/to/hello.py
directory=/path/to
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/path/to/hello.log
然後重新載入配置:
sudo supervisorctl reread
sudo supervisorctl update
這個示例將Python腳本添加為supervisor進程,當supervisord服務運行時會自動啟動hello.py。
四、總結
Supervisor是一個非常方便的進程監控與控制工具,通過上述步驟可以輕鬆安裝並使用它。使用它可以讓我們更加輕鬆地管理和監控一些常駐進程。
原創文章,作者:BNADC,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/371384.html