前言:出處
零、supervisor是什麼
Supervisor是用Python開發的一套通用的程式管理程式,能將一個普通的命令列程式變為後臺daemon,並監控程式狀態,異常退出時能自動重啟。它是通過fork/exec的方式把這些被管理的程式當作supervisor的子程式來啟動,這樣只要在supervisor的配置檔案中,把要管理的程式的可執行檔案的路徑寫進去即可。也實現當子程式掛掉的時候,父程式可以準確獲取子程式掛掉的資訊的,可以選擇是否自己啟動和報警。
一、安裝
apt -y install supervisor
yum -y install supervisor
二、配置
[unix_http_server]
file=/var/run/supervisor.sock
chmod=0700
[inet_http_server]
port=0.0.0.0:7020
username=root
password=xxxxxx
[supervisord]
logfile=/var/log/supervisor/supervisord.log
pidfile=/var/run/supervisord.pid
childlogdir=/var/log/supervisor
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock
[include]
files = /etc/supervisor/conf.d/*.conf
三、管理服務
需要前臺執行交於supervisor控制,原理是fork一個守護程式進行監控
program 標識,比如php-fpm,可以在後臺supervisorctl中進行start/stop/restart操作
command 命令,需要加上sleep 1,防止程式還沒退出supervisor就去檢測,還挺好用
1、管理php-fpm
[program:php-fpm]
command=bash -c "sleep 1 && sudo /usr/local/php7.4/sbin/php-fpm"
process_name=%(program_name)s
autostart=true
autorestart=true
startretries=5
exitcodes=0,2,70
stopsignal=QUIT
stopwaitsecs=2
stdout_logfile=/var/log/supervisor/php-fpm.log
ps -ef|grep php-fpm
root 23034 22175 0 23:37 ? 00:00:00 sudo /usr/local/php7.4/sbin/php-fpm
root 23043 23034 0 23:37 ? 00:00:00 php-fpm: master process (/usr/local/php7.4/etc/php-fpm.conf)
www-data 23044 23043 0 23:37 ? 00:00:00 php-fpm: pool www
www-data 23045 23043 0 23:37 ? 00:00:00 php-fpm: pool www
2、管理nginx
[program:nginx]
command=bash -c "sleep 1 && sudo /usr/local/nginx/sbin/nginx -g 'daemon off;'"
process_name=%(program_name)s
autostart=true
autorestart=true
startretries=5
exitcodes=0,2,70
stopsignal=INT
stopwaitsecs=2
stdout_logfile=/var/log/supervisor/nginx.log
ps -ef|grep nginx
root 22613 22175 0 23:35 ? 00:00:00 sudo /usr/local/nginx/sbin/nginx -g daemon off;
root 22616 22613 0 23:35 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -g daemon off;
www-data 22617 22616 0 23:35 ? 00:00:00 nginx: worker process
www-data 22618 22616 0 23:35 ? 00:00:00 nginx: worker process
www-data 22619 22616 0 23:35 ? 00:00:00 nginx: worker process
www-data 22620 22616 0 23:35 ? 00:00:00 nginx: worker process
四、supervisorctl
supervisorctl
supervisor> status
nginx RUNNING pid 22613, uptime 0:03:40
php-fpm RUNNING pid 23034, uptime 0:01:43
supervisor> ?
default commands (type help <topic>):
=====================================
add exit open reload restart start tail
avail fg pid remove shutdown status update
clear maintail quit reread signal stop version
五、參考資料
本作品採用《CC 協議》,轉載必須註明作者和本文連結