網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
一、簡(jiǎn)介
Supervisor 是一款 Python 開(kāi)發(fā)的進(jìn)程管理系統(tǒng),允許用戶(hù)監(jiān)視和控制 Linux 上的進(jìn)程,能將一個(gè)普通命令行進(jìn)程變?yōu)楹笈_(tái)守護(hù)進(jìn)程,異常退出時(shí)能自動(dòng)重啟
詳細(xì)介紹查閱:Supervisor Introduction
二、安裝
Supervisor 支持 Linux 和 Mac,不支持 Windows
本文系統(tǒng)為: centos,supervisor==4.2.4
pip3 install supervisor
安裝完成后,在Python bin目錄
下面會(huì)有這樣幾個(gè)文件。
ls /usr/local/Python38/bin echo_supervisord_conf supervisorctl supervisord
注:由于python bin目錄是加入了環(huán)境變量,所以可以直接執(zhí)行這幾個(gè)文件。
三、創(chuàng)建配置文件
1.首先建個(gè)目錄,存放配置文件:mkdir supervisord
。
echo_supervisord_conf > supervisord/supervisord.conf
若報(bào)錯(cuò) -bash: /etc/supervisord.conf: Permission denied,需要切換到 root用戶(hù)。
2.創(chuàng)建子進(jìn)程配置文件路徑
mkdir -p supervisor/conf.d
后面我們的任務(wù),我們想把它作為守護(hù)進(jìn)程,都需要一個(gè)配置文件,我們把這些配置文件放在conf.d目錄下面。
3.修改配置文件
vim supervisord/supervisord.conf
將最后一部分改為
[include] # 因?yàn)槲疫@里是放在root用戶(hù)目錄下,也可以放其它目錄 files=/root/supervisord/conf.d/*.conf
四、初試
1.啟動(dòng) supervisord
supervisord -c supervisord/supervisord.conf
這樣就把supervisord
啟動(dòng)了,我們接下來(lái)可以把我們的任務(wù)交給他去守護(hù)了,停止了會(huì)幫我們自動(dòng)重啟。
查看版本
supervisord -v
2.編寫(xiě)簡(jiǎn)單的 Shell 腳本
vim supervisord/test.sh
內(nèi)容如下
#!/bin/bash while : do echo `date '+%Y-%m-%d %H:%m:%S'` sleep 1 done
簡(jiǎn)單運(yùn)行,Ctrl + C
退出
3.創(chuàng)建子進(jìn)程配置文件
vim supervisor/conf.d/test.conf
test.conf
內(nèi)容如下:
[program:test] command=sh /root/supervisord/test.sh priority=999 ; 相對(duì)啟動(dòng)優(yōu)先級(jí),數(shù)值越小越優(yōu)先,默認(rèn)為999 autostart=true ; 在supervisor啟動(dòng)時(shí)自動(dòng)啟動(dòng),默認(rèn)為true autorestart=true ; 在意外退出時(shí)重新啟動(dòng),默認(rèn)為true startsecs=10 ; 子進(jìn)程啟動(dòng)多少秒后狀態(tài)為running則認(rèn)為啟動(dòng)成功,默認(rèn)為1 startretries=3 ; 嘗試啟動(dòng)的最大次數(shù),默認(rèn)為3 exitcodes=0,2 ; 進(jìn)程的預(yù)期退出代碼列表,默認(rèn)為0 stopsignal=QUIT ; 終止進(jìn)程的信號(hào),默認(rèn)為T(mén)ERM stopwaitsecs=10 ; 在SIGKILL之前等待的最大秒數(shù),默認(rèn)為10 user=root ; 在某用戶(hù)下設(shè)置uid來(lái)啟動(dòng)程序,默認(rèn)不切換用戶(hù) redirect_stderr=true ; 是否重定向stdout和stderr,默認(rèn)為false stdout_logfile=/tmp/supervisor.stdout.log ; stdout的輸出文件,默認(rèn)為AUTO stdout_logfile_maxbytes=50MB ; stdout最大文件大小,默認(rèn)為50MB stdout_logfile_backups=10 ; stdout文件備份數(shù),設(shè)為0則不備份,默認(rèn)為10
其實(shí)只需要配置3個(gè)參數(shù),其它不用管:
-
command=sh /root/supervisord/test.sh
: 我們的子進(jìn)程啟動(dòng)命令; -
stdout_logfile=/tmp/supervisor.stdout.log
: 日志; -
program:test
:進(jìn)程名為test
,如果進(jìn)程哪一天想停止啟動(dòng)等,需要進(jìn)程名;
目前文件目錄結(jié)構(gòu)是這樣的:
yum install tree tree supervisord supervisord ├── conf.d │ └── test.conf ├── supervisord.conf └── test.sh
4.重新讀取配置并更新子進(jìn)程
因?yàn)槲覀兊?code>supervisord已經(jīng)啟動(dòng)了,可以通過(guò)ps -ef | grep supervisord.conf
查看。添加了子進(jìn)程配置文件,需要把它重新加載進(jìn)來(lái):
先進(jìn)入supervisord
目錄: cd supervisord
,否則執(zhí)行下面命令會(huì)有問(wèn)題。
supervisorctl reread
再次查看進(jìn)程狀態(tài)
supervisorctl status
結(jié)果:
test ? ? RUNNING ? pid 30278, uptime 1:29:41
名為test
的這個(gè)進(jìn)程已經(jīng)作為守護(hù)進(jìn)程在后臺(tái)運(yùn)行,我們來(lái)kill掉他:
kill 30278
再執(zhí)行supervisorctl status
,會(huì)發(fā)現(xiàn)狀態(tài)立馬由starting
,不一會(huì)變成running
,那么supervisord
的作用已經(jīng)很明顯了,可以自動(dòng)幫我們自動(dòng)監(jiān)控任務(wù)。
注:對(duì)于子進(jìn)程的添加、刪除、啟動(dòng)、停止相關(guān)命令,見(jiàn)附錄。
五、Web 界面
web界面沒(méi)多大用處,就是如果想啟動(dòng)、暫停進(jìn)程是,不需要敲命令而已。
vim supervisord.conf
取消注釋
[inet_http_server] port=*:9001 ; 此處改為*便于調(diào)試
重啟 supervisord
supervisorctl reload
瀏覽器訪(fǎng)問(wèn):linux_ip:9001.
附錄:supervisorctl 常用命令
新增某配置文件,重新加載
supervisorctl reread
改動(dòng)某配置文件,重新加載
supervisorctl update
重啟 supervisord
supervisorctl reload
查看所有進(jìn)程狀態(tài)
supervisorctl status
查看指定進(jìn)程狀態(tài)
supervisorctl status <name>
啟動(dòng)所有子進(jìn)程
supervisorctl start all
啟動(dòng)指定子進(jìn)程
supervisorctl start <name>
重啟所有子進(jìn)程
supervisorctl restart all
重啟指定子進(jìn)程
supervisorctl restart <name>
停止所有子進(jìn)程
supervisorctl stop all
停止指定子進(jìn)程
supervisorctl stop <name>
添加子進(jìn)程到進(jìn)程組
supervisorctl add <name>
從進(jìn)程組移除子進(jìn)程,需要先stop。注意:移除后,需要使用reread和update才能重新運(yùn)行該進(jìn)程
supervisorctl reomve <name>
歡迎訪(fǎng)問(wèn)我的個(gè)人博客,聽(tīng)聽(tīng)我的故事。
原文鏈接:https://www.cnblogs.com/data-magnifier/p/16476920.html
相關(guān)推薦
- 2023-07-25 使用Redis做Mybatis的二級(jí)緩存
- 2023-04-19 Invalid prop: custom validator check failed for pr
- 2023-06-18 Redis優(yōu)雅地實(shí)現(xiàn)延遲隊(duì)列的方法分享_Redis
- 2022-12-02 Jetpack?Compose自定義動(dòng)畫(huà)與Animatable詳解_Android
- 2022-04-21 Ubuntu16.04系統(tǒng)搭建.Net?Core開(kāi)發(fā)環(huán)境_實(shí)用技巧
- 2023-03-27 python中的正則表達(dá)式,貪婪匹配與非貪婪匹配方式_python
- 2022-05-23 Android實(shí)現(xiàn)手機(jī)聯(lián)系人分欄效果_Android
- 2022-06-02 Python導(dǎo)包模塊報(bào)錯(cuò)的問(wèn)題解決_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支