supervisor簡介
Supervisor是用Python開發(fā)的一套通用的進程管理程序,能將一個普通的命令行進程變?yōu)楹笈_daemon,并監(jiān)控進程狀態(tài),異常退出時能自動重啟。它是通過fork/exec的方式把這些被管理的進程當作supervisor的子進程來啟動,這樣只要在supervisor的配置文件中,把要管理的進程的可執(zhí)行文件的路徑寫進去即可。也實現當子進程掛掉的時候,父進程可以準確獲取子進程掛掉的信息的,可以選擇是否自己啟動和報警。supervisor還提供了一個功能,可以為supervisord或者每個子進程,設置一個非root的user,這個user就可以管理它對應的進程
安裝
sudo apt-get install supervisor
配置
1.修改配置
; supervisor config file
[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file)
chmod=0700 ; sockef file mode (default 0700)
[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]
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 = /etc/supervisor/conf.d/*.conf
2.復制
安裝成功后的supervisor會在/etc下生成一個supervisor目錄和supervisor.conf文件,默認/etc/supervisor.conf為空,需要將/etc/supervisor/supervisor.conf的內容復制一份到/etc/supervisor.conf中,否則啟動不了。
3.編寫隊列配置
默認情況下,supervisor會讀取/etc/supervisor/conf.d下的所有.conf文件配置
[program:laravel-echo-server]
process_name=%(program_name)s_%(process_num)02d
command = laravel-echo-server start --force
directory = /var/www/html/echo/
autostart = true
autorestart=true
user=root
numprocs=1
redirect_stderr=true
stdout_logfile_maxbytes=25MB
stderr_logfile_backups=2
查看、啟動、停止、重啟supervisor進程
1、更新配置后必須執(zhí)行更新命令才生效
sudo supervisorctl update
2、查看supervisor進程
sudo supervisorctl status
3、啟動某個supervisor進程
sudo supervisorctl start xxxx
4、停止某個supervisor進程
sudo supervisorctl stop xxxx
5、停止所有supervisor進程
sudo supervisorctl stop all
6、重啟某個supervisor進程
sudo supervisorctl restart xxxx