Supervisor 守護(hù)linux 監(jiān)聽進(jìn)程

- supervisord

運(yùn)行 Supervisor 時(shí)會(huì)啟動(dòng)一個(gè)進(jìn)程 supervisord,它負(fù)責(zé)啟動(dòng)所管理的進(jìn)程,并將所管理的進(jìn)程作為自己的子進(jìn)程來啟動(dòng),而且可以在所管理的進(jìn)程出現(xiàn)崩潰時(shí)自動(dòng)重啟。

- supervisorctl

是命令行管理工具,可以用來執(zhí)行 stop、start、restart 等命令,來對這些子進(jìn)程進(jìn)行管理。

supervisor是所有進(jìn)程的父進(jìn)程,管理著啟動(dòng)的子進(jìn)展,supervisor以子進(jìn)程的PID來管理子進(jìn)程,當(dāng)子進(jìn)程異常退出時(shí)supervisor可以收到相應(yīng)的信號(hào)量。

安裝python

yum install python-setuptools

安裝Supervisor?

方法1:

easy_install supervisor

方法2:

wget https://pypi.python.org/packages/source/s/supervisor/supervisor-3.1.3.tar.gz

tar zxvf supervisor-3.1.3.tar.gz

cd supervisor-3.1.3python setup.py install

查看安裝是否成功

echo_supervisord_conf

創(chuàng)建目錄,初始化配置文件

mkdir /usr/supervisor

echo_supervisord_conf > /usr/supervisor/supervisord.conf ---生成配置文件

echo_supervisord_conf詳解:

[root@centos-011 ~ 07:50:00]

; Sample supervisor config file.

[unix_http_server]

file=/var/run/supervisor/supervisor.sock? ; socket 路徑

;chmod=0700? ? ? ? ? ? ? ? ; socket 文件的權(quán)限

;chown=nobody:nogroup? ? ? ; socket 所屬用戶及組

;username=user? ? ? ? ? ? ? ; 用戶名

;password=123? ? ? ? ? ? ? ; 密碼

;[inet_http_server]? ? ? ? ; 是否啟用服務(wù),默認(rèn)是關(guān)閉的(啟用的話可以看到supervisor 管理的服務(wù)狀態(tài))

;port=127.0.0.1:9001? ? ? ? ; 監(jiān)聽的IP及端口

;username=user? ? ? ? ? ? ? ; 用戶名

;password=123? ? ? ? ? ? ? ; 密碼

[supervisord]? ? ? ? ? ? ? ; supervisord 全局配置

logfile=/var/log/supervisor/supervisord.log? ; supervisor 日志路徑

logfile_maxbytes=50MB? ? ? ; 單個(gè)日志文件最大數(shù)

logfile_backups=10? ? ? ? ? ; 保留多少個(gè)日志文件(默認(rèn)10個(gè))

loglevel=info? ? ? ? ? ? ? ; (log level;default info; others: debug,warn,trace)

pidfile=/var/run/supervisord.pid ; pid 文件路徑

nodaemon=false? ? ? ? ? ? ? ; 啟動(dòng)是否丟到前臺(tái),設(shè)置為false ,表示以daemon 的方式啟動(dòng)

minfds=1024? ? ? ? ? ? ? ? ; 最小文件打開數(shù),對應(yīng)系統(tǒng)limit.conf 中的nofile ,默認(rèn)最小為1024,最大為4096

minprocs=200? ? ? ? ? ? ? ? ; 最小的進(jìn)程打開數(shù),對應(yīng)系統(tǒng)的limit.conf 中的nproc,默認(rèn)為200

;umask=022? ? ? ? ? ? ? ? ? ; (process file creation umask;default 022)

;user=chrism? ? ? ? ? ? ? ? ; 啟動(dòng)supervisord 服務(wù)的用戶,默認(rèn)為root

;identifier=supervisor? ? ? ; (supervisord identifier, default is 'supervisor')

;directory=/tmp? ? ? ? ? ? ? ; 這里的目錄指的是服務(wù)的工作目錄

;nocleanup=true? ? ? ? ? ? ? ; (don't clean up tempfiles at start;default false)

;childlogdir=/tmp? ? ? ? ? ? ; ('AUTO' child log dir, default $TEMP)

;environment=KEY=value? ? ? ; (key value pairs to add to environment)

;strip_ansi=false? ? ? ? ? ? ; (strip ansi escape codes in logs; def. false)

; 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/supervisor.sock ; use a unix:// URL? for a unix socket

;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket

;username=chris? ? ? ? ? ? ? ; should be same as http_username if set

;password=123? ? ? ? ? ? ? ? ; should be same as http_password if set

;prompt=mysupervisor? ? ? ? ; cmd line prompt (default "supervisor")

;history_file=~/.sc_history? ; use readline history if available

; The below sample program section shows all possible program subsection values,

; create one or more 'real' program: sections to be able to control them under

; supervisor.

;[program:theprogramname]? ? ? ; 定義一個(gè)守護(hù)進(jìn)程 ,比如下面的elasticsearch

;command=/bin/cat? ? ? ? ? ? ? ; 啟動(dòng)程序使用的命令,可以是絕對路徑或者相對路徑

;process_name=%(program_name)s ; 一個(gè)python字符串表達(dá)式,用來表示supervisor進(jìn)程啟動(dòng)的這個(gè)的名稱,默認(rèn)值是%(program_name)s

;numprocs=1? ? ? ? ? ? ? ? ? ? ; Supervisor啟動(dòng)這個(gè)程序的多個(gè)實(shí)例,如果numprocs>1,則process_name的表達(dá)式必須包含%(process_num)s,默認(rèn)是1

;directory=/tmp? ? ? ? ? ? ? ? ; supervisord在生成子進(jìn)程的時(shí)候會(huì)切換到該目錄

;umask=022? ? ? ? ? ? ? ? ? ? ; umask for process (default None)

;priority=999? ? ? ? ? ? ? ? ? ; 權(quán)重,可以控制程序啟動(dòng)和關(guān)閉時(shí)的順序,權(quán)重越低:越早啟動(dòng),越晚關(guān)閉。默認(rèn)值是999

;autostart=true? ? ? ? ? ? ? ? ; 如果設(shè)置為true,當(dāng)supervisord啟動(dòng)的時(shí)候,進(jìn)程會(huì)自動(dòng)啟動(dòng)

;autorestart=true? ? ? ? ? ? ? ; 設(shè)置為隨 supervisord 重啟而重啟,值可以是false、true、unexpected。false:進(jìn)程不會(huì)自動(dòng)重啟

;startsecs=10? ? ? ? ? ? ? ? ? ; 程序啟動(dòng)后等待多長時(shí)間后才認(rèn)為程序啟動(dòng)成功,默認(rèn)是10秒

;startretries=3? ? ? ? ? ? ? ? ; supervisord嘗試啟動(dòng)一個(gè)程序時(shí)嘗試的次數(shù)。默認(rèn)是3

;exitcodes=0,2? ? ? ? ? ? ? ? ; 一個(gè)預(yù)期的退出返回碼,默認(rèn)是0,2。

;stopsignal=QUIT? ? ? ? ? ? ? ; 當(dāng)收到stop請求的時(shí)候,發(fā)送信號(hào)給程序,默認(rèn)是TERM信號(hào),也可以是 HUP, INT, QUIT, KILL, USR1, or USR2

;stopwaitsecs=10? ? ? ? ? ? ? ; 在操作系統(tǒng)給supervisord發(fā)送SIGCHILD信號(hào)時(shí)等待的時(shí)間

;user=chrism? ? ? ? ? ? ? ? ? ; 如果supervisord以root運(yùn)行,則會(huì)使用這個(gè)設(shè)置用戶啟動(dòng)子程序

;redirect_stderr=true? ? ? ? ? ; 如果設(shè)置為true,進(jìn)程則會(huì)把標(biāo)準(zhǔn)錯(cuò)誤輸出到supervisord后臺(tái)的標(biāo)準(zhǔn)輸出文件描述符

;stdout_logfile=/a/path? ? ? ? ; 把進(jìn)程的標(biāo)準(zhǔn)輸出寫入文件中,如果stdout_logfile沒有設(shè)置或者設(shè)置為AUTO,則supervisor會(huì)自動(dòng)選擇一個(gè)文件位置

;stdout_logfile_maxbytes=1MB? ; 標(biāo)準(zhǔn)輸出log文件達(dá)到多少后自動(dòng)進(jìn)行輪轉(zhuǎn),單位是KB、MB、GB。如果設(shè)置為0則表示不限制日志文件大小

;stdout_logfile_backups=10? ? ; 標(biāo)準(zhǔn)輸出日志輪轉(zhuǎn)備份的數(shù)量,默認(rèn)是10,如果設(shè)置為0,則不備份

;stdout_capture_maxbytes=1MB? ; 當(dāng)進(jìn)程處于stderr capture mode模式的時(shí)候,寫入FIFO隊(duì)列的最大bytes值,單位可以是KB、MB、GB

;stdout_events_enabled=false? ; 如果設(shè)置為true,當(dāng)進(jìn)程在寫它的stderr

;stderr_logfile=/a/path? ? ? ? ; 把進(jìn)程的錯(cuò)誤日志輸出一個(gè)文件中,除非redirect_stderr參數(shù)被設(shè)置為true

;stderr_logfile_maxbytes=1MB? ; 錯(cuò)誤log文件達(dá)到多少后自動(dòng)進(jìn)行輪轉(zhuǎn),單位是KB、MB、GB。如果設(shè)置為0則表示不限制日志文件大小

;stderr_logfile_backups=10? ? ; 錯(cuò)誤日志輪轉(zhuǎn)備份的數(shù)量,默認(rèn)是10,如果設(shè)置為0,則不備份

;stderr_capture_maxbytes=1MB? ; 當(dāng)進(jìn)程處于stderr capture mode模式的時(shí)候,寫入FIFO隊(duì)列的最大bytes值,單位可以是KB、MB、GB

;stderr_events_enabled=false? ; 如果設(shè)置為true,當(dāng)進(jìn)程在寫它的stderr到文件描述符的時(shí)候,PROCESS_LOG_STDERR事件會(huì)被觸發(fā)

;environment=A=1,B=2? ? ? ? ? ; 一個(gè)k/v對的list列表

;serverurl=AUTO? ? ? ? ? ? ? ? ; 是否允許子進(jìn)程和內(nèi)部的HTTP服務(wù)通訊,如果設(shè)置為AUTO,supervisor會(huì)自動(dòng)的構(gòu)造一個(gè)url

; The below sample eventlistener section shows all possible

; eventlistener subsection values, create one or more 'real'

; eventlistener: sections to be able to handle event notifications

; sent by supervisor.

#這個(gè)地方是自定義一個(gè)守護(hù)進(jìn)程

[program:elasticsearch]? ? ? ? ? ? ? ? ? ? ? ; 定義一個(gè)守護(hù)進(jìn)程 elasticsearch

environment=ES_HOME=/usr/local/elasticsearch? ; 設(shè)置ES_HOME 環(huán)境變量

user=elk? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ; 啟動(dòng)elasticsearch 的用戶

directory=/usr/local/elasticsearch? ? ? ? ? ? ; 進(jìn)入到這個(gè)目錄中

command=/usr/local/elasticsearch/bin/elasticsearch ; 執(zhí)行啟動(dòng)命令

numprocs=1? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ; Supervisor啟動(dòng)這個(gè)程序的多個(gè)實(shí)例,如果numprocs>1,則process_name的表達(dá)式必須包含%(process_num)s,默認(rèn)是1

autostart=true? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ; 設(shè)置為隨 supervisord 啟動(dòng)而啟動(dòng)

autorestart=true? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ; 設(shè)置為隨 supervisord 重啟而重啟

startretries=3? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ; 設(shè)置elasticsearch 重啟的重試次數(shù)

priority=1? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ; 權(quán)重,可以控制程序啟動(dòng)和關(guān)閉時(shí)的順序,權(quán)重越低:越早啟動(dòng),越晚關(guān)閉。默認(rèn)值是999?

;[eventlistener:theeventlistenername]

;command=/bin/eventlistener? ? ; 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)

;events=EVENT? ? ? ? ? ? ? ? ? ; event notif. types to subscribe to (req'd)

;buffer_size=10? ? ? ? ? ? ? ? ; event buffer queue size (default 10)

;directory=/tmp? ? ? ? ? ? ? ? ; directory to cwd to before exec (def no cwd)

;umask=022? ? ? ? ? ? ? ? ? ? ; umask for process (default None)

;priority=-1? ? ? ? ? ? ? ? ? ; the relative start priority (default -1)

;autostart=true? ? ? ? ? ? ? ? ; start at supervisord start (default: true)

;autorestart=unexpected? ? ? ? ; restart at unexpected quit (default: unexpected)

;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_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? ? ? ? ; # of stderr logfile backups (default 10)

;stderr_events_enabled=false? ; emit events on stderr writes (default false)

;environment=A=1,B=2? ? ? ? ? ; process environment additions

;serverurl=AUTO? ? ? ? ? ? ? ? ; override serverurl computation (childutils)

; The below sample group section shows all possible group values,

; create one or more 'real' group: sections to create "heterogeneous"

; process groups.

;[group:thegroupname]? ? ? ? ? ; 服務(wù)組管理,可以將多個(gè)服務(wù)名寫到這里管理(組名自定義)

;programs=progname1,progname2? ; 上面配置好的服務(wù)名,比如elasticsearch,kibana,logstash

;priority=999? ? ? ? ? ? ? ? ? ; the relative start priority (default 999)

; 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 = supervisord.d/*.ini


?為了不將所有新增配置信息全寫在一個(gè)配置文件里,這里新建一個(gè)文件夾

mkdir /usr/supervisor/supervisord.d/

?修改系統(tǒng)配置文件

vim /usr/supervisor/supervisord.conf

?include 中新建的supervisord.d文件夾下所有的conf

[include]

files = supervisord.d/*.conf? ?--管理所以進(jìn)程的配置文件

?若需要web查看進(jìn)程,則去掉[inet_http_server]的注釋

[inet_http_server]

port=127.0.0.1:9001? ;IP按需配置? ?

username=user? ? ? ? ? ? ?

password=123

啟動(dòng)supervisord

supervisord -c /usr/supervisor/supervisord.conf

3.1 查看監(jiān)聽

lsof -i:9001

就可以通過 http://ip:9001/ 查看supervisor的web界面了,只不過目前還沒有加入任何監(jiān)控程序


增加一個(gè)配置文件,以便supervisor用來監(jiān)控程序

cat > /usr/supervisor/supervisord.d/supervisor_test_one.conf

在新建的supervisor_test_one.conf文件中添加以下配置

[program:foundaction] ;程序名稱,終端控制時(shí)需要的標(biāo)識(shí)

command=nohup java -jar /home/foundation-management/ntbrick-foundation-management.jar --sprin

g.config.location=/home/foundation-management/application.yml & ;

directory=/home/foundation-management ; 命令執(zhí)行的目錄

numprocs=1 ; 啟動(dòng)幾個(gè)進(jìn)程

autorestart=true ; 程序意外退出是否自動(dòng)重啟

user=root ; 進(jìn)程執(zhí)行的用戶身份

重啟supervisor

方法1:supervisorctl -c /usr/supervisor/supervisord.conf

方法2:supervisorctl reload

更新配置

supervisorctl update

現(xiàn)在就可以在http://ip:9001/中訪問test_one的程序了。



開機(jī)啟動(dòng)

supervisor開機(jī)自動(dòng)啟動(dòng)

3.5.1 在目錄/usr/lib/systemd/system/ 新建文件supervisord.service,并添加配置內(nèi)容

[Unit]

Description=Process Monitoringand Control Daemon

After=rc-local.service nss-user-lookup.target

[Service]

Type=forking

ExecStart=/usr/bin/supervisord -c /usr/supervisor/supervisord.conf ;開機(jī)啟動(dòng)時(shí)執(zhí)行

ExecStop=/usr/bin/supervisord shutdown

ExecReload=/usr/bin/supervisord reload

killMode=process

Restart=on-failure

RestartSec=42s

[Install]

WantedBy=multi-user.target

? 啟動(dòng)服務(wù)

systemctl enable supervisord

? 驗(yàn)證一下是否為開機(jī)啟動(dòng)

systemctl is-enabled supervisord



superlance 發(fā)送意外退出郵件通知



nginx 由于supervisor不能監(jiān)控后臺(tái)程序,

command = /usr/local/bin/nginx 這個(gè)命令默認(rèn)是后臺(tái)啟動(dòng),

加上-g ‘daemon off;’這個(gè)參數(shù)可解決這問題,這個(gè)參數(shù)的意思是在前臺(tái)運(yùn)行。

command = /usr/local/bin/nginx -g ‘daemon off;’

[program:nginx]

command = /usr/sbin/nginx -g 'daemon off;'

startsecs=0

autostart=true

autorestart=true

stdout_logfile=/var/log/nginx_sup.log

stopasgroup=true

killasgroup=true`


redis?由于 Supervisor 管理的進(jìn)程不能設(shè)置為 daemon 模式,故如果 Redis 無法正常啟動(dòng),可以查看一下 Redis 的配置,并將daemonize選項(xiàng)設(shè)置為 no。

daemonize no


[program:zookeeper]

command=/usr/local/zookeeper/bin/zkServer.shstart-foreground

user=root

autostart=trueautorestart=truestartsecs=3

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容