這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
golang supervisor
go準備工作
建立一個<demo>項目,裡面main.go代碼
代碼摘自<Go Web 編程>
package mainimport ( "fmt" "log" "net/http" "strings")func sayhelloName(w http.ResponseWriter, r *http.Request) { r.ParseForm() //解析參數,預設是不會解析的 fmt.Println(r.Form) //這些資訊是輸出到伺服器端的列印資訊 fmt.Println("path", r.URL.Path) fmt.Println("scheme", r.URL.Scheme) fmt.Println(r.Form["url_long"]) for k, v := range r.Form { fmt.Println("key:", k) fmt.Println("val:", strings.Join(v, "")) } fmt.Fprintf(w, "Hello astaxie!") //這個寫入到w的是輸出到用戶端的}func main() { http.HandleFunc("/", sayhelloName) //設定訪問的路由 err := http.ListenAndServe(":9090", nil) //設定監聽的連接埠 if err != nil { log.Fatal("ListenAndServe: ", err) }}
go build
go install
本地測試通過之後把啟動並執行demo包傳至伺服器
安裝supervisor
官網地址
http://supervisord.org/index.html
sudo yum install python-setuptoolssudo easy_install supervisor
安裝成功後 組建組態檔案
sudo echo_supervisord_conf > /etc/supervisord.conf
這裡很簡單,如遇問題可以自己Google下
添加自己的設定檔
我也學網上在/etc/下面建立一個專門放 .conf 的檔案夾,感覺這樣很好,比一味修改supervisord.conf檔案要更方便以後管理
我這命名
"supervisorconffile"
在supervisorconffile中建立個.conf檔案
我的
demo.conf
[program:demo]user=rootcommand=/root/Applications/Go/bin/demoautostart=trueautorestart=truestartsecs=10stdout_logfile=/root/Applications/LogFile/log/demo.log stdout_logfile_maxbytes=1MBstdout_logfile_backups=10stdout_capture_maxbytes=1MBstderr_logfile=/root/Applications/LogFile/err/demo.logstderr_logfile_maxbytes=1MBstderr_logfile_backups=10stderr_capture_maxbytes=1MBstopsignal=INT[supervisord]
說明
command:表示啟動並執行命令,我這是填寫的我demo安裝包的原則路徑。autostart:表示是否跟隨supervisor一起啟動。autorestart:如果該程式掛了,是否重新啟動。stdout_logfile:終端標準輸出重新導向檔案。stderr_logfile:終端錯誤輸出重新導向檔案。
注意上面的兩個log檔案
/root/Applications/LogFile/log/demo.log
/root/Applications/LogFile/err/demo.log
都要在相應目錄下面建立對應的log
修改設定檔
好了開始
編輯/etc/supervisord.conf
在檔案最下面
剛開啟是這樣的
;[include];files = relative/directory/*.ini
改成這樣
[include]files = /etc/supervisorconffile/*.conf
注意[include]前面的';'要去掉,我在這點上耽誤了點時間
啟動
sudo /usr/bin/supervisord -c /etc/supervisord.conf
報錯了
Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.
網上查到執行下面兩條命令可以解決
1.find / -name supervisor.sock2.unlink /***/supervisor.sock
再次執行
sudo /usr/bin/supervisord -c /etc/supervisord.conf
啟動成功
我這成功什麼都沒有列印輸出反饋,沒有反饋就是成功了沒毛病了
可以輸入命令查看下剛剛啟動的demo服務是否是成功
sudo supervisorctl status demo
輸出
demo STARTING
查看狀態 輸入名錄
supervisorctl
輸出:
demo RUNNING pid 4806, uptime 0:04:05
supervisor> help
用help查看協助
輸入exit退出