1、介紹Supervisord軟件
一、什麼是Supervisord?
Supervisord是一個很是優秀的進程管理工具,使用Python開發。它能夠在類UNIX系統的方式讓用戶來準確地監視和控制後臺必定數量的服務進程。並做爲一個天使進程讓後臺進程在當發生內部錯誤退出、或者進程被意外殺死時自動重啓。除此以外,supervisord能夠監控TCP端口,讓其餘主機經過客戶端了命令supervisorctl經過HTTP協議直接對Server端進程進行啓停,避免讓進程/服務器管理者直接接觸Shell或root用戶。進程之間也有一個優先級和進程組關係,讓管理員使用start all和stop all的關係來啓動。web
二、安裝Supervisord軟件shell
[root@yyljxl ~]# cat /etc/redhat-release
CentOS Linux release 7.xvim
[root@www ]# yum -y install supervisor
[root@yyljxl ~]# supervisorctl -h
supervisorctl -- control applications run by supervisord from the cmd line.bash
Usage: /usr/bin/supervisorctl [options] [action [arguments]]服務器
Options:
-c/--configuration -- configuration file path (default /etc/supervisord.conf)
-h/--help -- print usage message and exit
-i/--interactive -- start an interactive shell after executing commands
-s/--serverurl URL -- URL on which supervisord server is listening
(default "http://localhost:9001").
-u/--username -- username to use for authentication with server
-p/--password -- password to use for authentication with server
-r/--history-file -- keep a readline history (if readline is available)app
action [arguments] -- see belowtcp
Actions are commands like "tail" or "stop". If -i is specified or no action is
specified on the command line, a "shell" interpreting actions typed
interactively is started. Use the action "help" to find out about available
actions.
出現上面的幫助信息表示安裝成功了。函數
#啓動服務
systemctl start supervisord
systemctl enable supervisord
systemctl status supervisord工具
#supervisord監控的軟件的配置說明
vim /etc/supervisor/conf.d
[program:項目名]
command=/data/www/go/src/test/test.bin
程序啓動命令
autostart=true
在supervisord啓動的時候也自動啓動
startsecs=10
啓動10秒後沒有異常退出,就表示進程正常啓動了,默認爲1秒
autorestart=true
程序退出後自動重啓,可選值:[unexpected,true,false],默認爲unexpected,表示進程意外殺死後才重啓
startretries=3
啓動失敗自動重試次數,默認是3
user=root
用哪一個用戶啓動進程,默認是root
priority=999
進程啓動優先級,默認999,值小的優先啓動
redirect_stderr=true
把stderr重定向到stdout,默認false
stdout_logfile_maxbytes=20MB
stdout 日誌文件大小,默認50MB
stdout_logfile_backups = 20
stdout 日誌文件備份數,默認是10
stdout 日誌文件,須要注意當指定目錄不存在時沒法正常啓動,因此須要手動建立目錄(supervisord 會自動建立日誌文件)
stdout_logfile=/data/logs/test/test.log
日誌輸出的文件地址
stopasgroup=false
默認爲false,進程被殺死時,是否向這個進程組發送stop信號,包括子進程
killasgroup=false
默認爲false,向進程組發送kill信號,包括子進程。測試
#官方文檔
http://supervisord.org/
三、使用案例
示例:編寫一個簡單的go web項目
[root@web scripts]# cat mail.go
package main
import (
"fmt"
"net/http"
)
//w, 給客戶端回覆數據
//r, 讀取客戶端發送的數據
func HandConn(w http.ResponseWriter, r *http.Request) {
fmt.Println("r.Method = ", r.Method)
fmt.Println("r.URL = ", r.URL)
fmt.Println("r.Header = ", r.Header)
fmt.Println("r.Body = ", r.Body)
w.Write([]byte("hello go")) //給客戶端回覆數據
}
func main() {
//註冊處理函數,用戶鏈接,自動調用指定的處理函數
http.HandleFunc("/", HandConn)
//監聽綁定
http.ListenAndServe(":8000", nil)
}
#上傳程序到Centos7.x系統
yum install go -y
#build程序
[root@yyljxl scripts]# go build -o test
#生成可執行文件
[root@yyljxl scripts]# ll
total 6416
-rw-r--r-- 1 root root 542 Jun 24 09:15 mail.go
-rwxr-xr-x 1 root root 6562362 Jun 24 09:37 test
#運行程序
[root@yyljxl scripts]# nohup ./test &
[1] 14132
[root@yyljxl scripts]# nohup: ignoring input and appending output to ‘nohup.out’
#查看進程
[root@yyljxl ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp6 0 0 :::8000 :::* LISTEN 14132/ ./test
四、配置test程序用supervisord監控
[root@www scripts]#
mkdir -p /nulige/scripts/logs
touch /nulige/scripts/logs/test.log
#配置supervisor監控test
cat > /etc/supervisord.d/test.ini <<EOF
[program:test]
command=/nulige/scripts/test
autostart=true
startsecs=10
autorestart=true
startretries=3
user=root
priority=999
redirect_stderr=true
stdout_logfile_maxbytes=20MB
stdout_logfile_backups = 20
stdout_logfile=/nulige/scripts/logs/test.log
stopasgroup=false
killasgroup=false
EOF
#執行命令
[root@yyljxl scripts]# supervisorctl update
test: added process group
[root@yyljxl scripts]# supervisorctl status
test RUNNING pid 15214, uptime 0:00:09
#端口占用,致使沒有載入成功,kill掉原端口程序,再從新載入
[root@yyljxl scripts]# supervisorctl status
test FATAL Exited too quickly (process log may have details)
#從新載入
[root@yyljxl scripts]# supervisorctl reload
Restarted supervisord
#更新監控的程序
[root@yyljxl scripts]# supervisorctl update
#查看運行狀態
[root@yyljxl scripts]# supervisorctl status
test RUNNING pid 15214, uptime 0:00:09
#中止服務
[root@yyljxl scripts]# supervisorctl stop test
test: stopped
#測試殺當前程序的pid,再觀察pid變化。
#查看進程 [root@yyljxl ~]# netstat -lntup Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp6 0 0 :::8000 :::* LISTEN 15385/test #殺進程 [root@yyljxl ~]# kill 15385 [root@yyljxl ~]# kill 15385 -bash: kill: (15385) - No such process #查看進程,發現PID已變化,說明監控成功,服務中止後,會自啓動。 [root@yyljxl ~]# netstat -lntup Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp6 0 0 :::8000 :::* LISTEN 15416/test
五、supervisord經常使用命令
supervisorctl status #監控的程序的運行狀態的列表supervisorctl stop test #中止監控的程序supervisorctl start test #啓動監控的程序supervisorctl restart test #重啓監控的程序supervisorctl update #更新監控的程序,若有新的配置添加必定要先執行updatesupervisorctl reload #載入最新的配置文件,並按新的配置啓動、管理全部進程。