簡介
centos有自帶的supervisor,可以通過yum直接安裝。
安裝
在CentOS7上可以直接安裝,在CentOS Stream上可以通過epel-release安裝。
yum -y install supervisor
服務
CentOS7以上的版本都是使用systemd進行服務管理
systemctl enable supervisord
systemctl start supervisord
配置
默認配置文件是/etc/supervisord.conf
[unix_http_server]
file=/run/supervisor/supervisor.sock ; (the path to the socket file)
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///run/supervisor/supervisor.sock ; use a unix:// URL for a unix socket
[include]
files = supervisord.d/*.ini
[root@centos8 ~]# cat /etc/supervisord.conf | grep -v '^;' | grep -v '^$'
[unix_http_server]
file=/run/supervisor/supervisor.sock ; (the path to the socket file)
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///run/supervisor/supervisor.sock ; use a unix:// URL for a unix socket
[include]
files = supervisord.d/*.ini
可以直接修改這個配置直接加promgram管理程序
也可以放到/etc/supervisord.d/*.ini這個目錄里面,獨立文件方便拷貝管理。
vim /etc/supervisord.d/demo.ini
[program:demo]
command=/usr/local/demo/demo ; 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=/usr/local/demo ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=999 ; the relative start priority (default 999)
;autostart=true ; start at supervisord start (default: true)
;autorestart=true ; retstart at unexpected quit (default: true)
;startsecs=10 ; number of secs prog must stay running (def. 1)
;startretries=3 ; max # of serial start failures (default 3)
;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 b4 SIGKILL (default 10)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=true ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)
;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A=1,B=2 ; process environment additions (def no adds)
;serverurl=AUTO ; override serverurl computation (childutils)
配置好程序路徑就可以加載到任務了。
任務管理
可以通過supervisorctl進行任務控制
supervisorctl status #查看進程運行狀態
supervisorctl start 進程名 #啟動進程
supervisorctl stop 進程名 #關閉進程
supervisorctl restart 進程名 #重啟進程
supervisorctl shutdown #關閉supervisord
supervisorctl reread #重新載入配置文件
supervisorctl reload #重新載入配置并重啟
句柄不夠
在一些高并發的程序可能出現句柄不夠用的情況,可以通過編輯supervisord.service文件修改句柄數。
vim /usr/lib/systemd/system/supervisord.service
[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service
[Service]
LimitNOFILE=40960
LimitNPROC=40960
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
RuntimeDirectory=supervisor
RuntimeDirectoryMode=755
[Install]
WantedBy=multi-user.target
修改之后需要重啟服務生效
systemctl daemon-reload
systemctl restart supervisord
總結
使用supervisor可以讓我們快速管理服務,特別是部署Go程序的時候很方便。