從零部署 supervisor 維護(hù) golang 進(jìn)程

golang supervisor

go準(zhǔn)備工作

創(chuàng)建一個(gè)<demo>項(xiàng)目,里面main.go代碼
代碼摘自<Go Web 編程>

package main

import (
    "fmt"
    "log"
    "net/http"
    "strings"
)

func sayhelloName(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()       //解析參數(shù),默認(rèn)是不會(huì)解析的
    fmt.Println(r.Form) //這些信息是輸出到服務(wù)器端的打印信息
    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!") //這個(gè)寫(xiě)入到w的是輸出到客戶端的
}

func main() {
    http.HandleFunc("/", sayhelloName)       //設(shè)置訪問(wèn)的路由
    err := http.ListenAndServe(":9090", nil) //設(shè)置監(jiān)聽(tīng)的端口
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

go build
go install
本地測(cè)試通過(guò)之后把運(yùn)行的demo包傳至服務(wù)器

安裝supervisor

官網(wǎng)地址
http://supervisord.org/index.html

sudo yum install python-setuptools
sudo easy_install supervisor

安裝成功后 生成配置文件

sudo echo_supervisord_conf > /etc/supervisord.conf

這里很簡(jiǎn)單,如遇問(wèn)題可以自己Google下

添加自己的配置文件

我也學(xué)網(wǎng)上在/etc/下面新建一個(gè)專門(mén)放 .conf 的文件夾,感覺(jué)這樣很好,比一味修改supervisord.conf文件要更方便以后管理
我這命名
"supervisorconffile"
在supervisorconffile中新建個(gè).conf文件
我的
demo.conf

[program:demo]
user=root
command=/root/Applications/Go/bin/demo
autostart=true
autorestart=true
startsecs=10
stdout_logfile=/root/Applications/LogFile/log/demo.log 
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=10
stdout_capture_maxbytes=1MB
stderr_logfile=/root/Applications/LogFile/err/demo.log
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
stderr_capture_maxbytes=1MB
stopsignal=INT
[supervisord] 

說(shuō)明

command:表示運(yùn)行的命令,我這是填寫(xiě)的我demo安裝包的原則路徑。
autostart:表示是否跟隨supervisor一起啟動(dòng)。
autorestart:如果該程序掛了,是否重新啟動(dòng)。
stdout_logfile:終端標(biāo)準(zhǔn)輸出重定向文件。
stderr_logfile:終端錯(cuò)誤輸出重定向文件。

注意上面的兩個(gè)log文件
/root/Applications/LogFile/log/demo.log
/root/Applications/LogFile/err/demo.log
都要在相應(yīng)目錄下面創(chuàng)建對(duì)應(yīng)的log

修改配置文件

好了開(kāi)始
編輯/etc/supervisord.conf

在文件最下面
剛打開(kāi)是這樣的

;[include]
;files = relative/directory/*.ini

改成這樣

[include]
files = /etc/supervisorconffile/*.conf

注意[include]前面的';'要去掉,我在這點(diǎn)上耽誤了點(diǎn)時(shí)間

啟動(dòng)

sudo /usr/bin/supervisord -c /etc/supervisord.conf

報(bào)錯(cuò)了
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.

網(wǎng)上查到執(zhí)行下面兩條命令可以解決

1.
find / -name supervisor.sock
2.
unlink /***/supervisor.sock

再次執(zhí)行
sudo /usr/bin/supervisord -c /etc/supervisord.conf
啟動(dòng)成功
我這成功什么都沒(méi)有打印輸出反饋,沒(méi)有反饋就是成功了沒(méi)毛病了

可以輸入命令查看下剛剛啟動(dòng)的demo服務(wù)是否是成功

sudo supervisorctl status demo

輸出

demo                             STARTING 

查看狀態(tài) 輸入名錄

supervisorctl

輸出:

demo                             RUNNING   pid 4806, uptime 0:04:05

supervisor> help
用help查看幫助
輸入exit退出

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

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

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