在CentOS7上用Supervisor運行Golang守護進程

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

安裝pip

下載pip安裝檔案並執行安裝

  • 下載檔案
    [root@snails ~]# wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate
  • 執行安裝
    [root@snails ~]# python get-pip.py[root@snails ~]# pip -Vpip 8.1.2 from /usr/lib/python2.7/site-packages (python 2.7)

設定pip國內aliyun源

  • 建立或修改設定檔
    linux ~/.pip/pip.confwindows %HOMEPATH%\pip\pip.ini
  • 修改內容

    [global]  index-url = http://mirrors.aliyun.com/pypi/simple/  [install]trusted-host=mirrors.aliyun.com
  • 更新pip到最新版本
    [root@snails ~]# pip install -U pip
  • 查看已安裝的庫
    [root@snails ~]# pip list

安裝supervisor

安裝

[root@snails ~]# pip install supervisor

安裝成功便可以擁有Supervisor,如果沒有啟動指令碼,可以從 這裡 下載一份,放置到 /usr/lib/systemd/system 或 /etc/systemd/system 目錄(後者優先順序更高)下面便可。

[root@snails ~]# wget https://raw.githubusercontent.com/Supervisor/initscripts/master/centos-systemd-etcs -O /usr/lib/systemd/system/systemd-supervisor.service

配置

通過Supervisor附送的貼心的小指令碼產生預設的設定檔

[root@snails ~]# echo_supervisord_conf > /etc/supervisord.conf

 我們可以根據需要修改裡面的配置。我這裡,每個不同的項目,使用了一個單獨的配置的檔案,放置在 /etc/supervisor/下面,於是修改 /etc/supervisord.conf ,加上如下內容:

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

建立golang http服務

為了測試方便,我這裡用一個最簡單的golang http服務。

[root@snails ~]# vi ~/simple_http_server.go
package main import (    "fmt"    "log"    "net/http") func main() {    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {        fmt.Fprintf(w, "Hello world\n")    })    err := http.ListenAndServe(":9090", nil)    if err != nil {        log.Fatal("ListenAndServe: ", err)    }}

直接運行這個程式會佔用住終端,下面看看如何用supervisor來跑這個程式。

建立golang服務對應的supervisor設定檔

vi /etc/supervisor/golang.conf[program:golang-http-server]command=/root/simple_http_serverautostart=trueautorestart=truestartsecs=10stdout_logfile=/var/log/simple_http_server.logstdout_logfile_maxbytes=1MBstdout_logfile_backups=10stdout_capture_maxbytes=1MBstderr_logfile=/var/log/simple_http_server.logstderr_logfile_maxbytes=1MBstderr_logfile_backups=10stderr_capture_maxbytes=1MB
  • 幾個配置說明:
    command:表示啟動並執行命令,填入完整的路徑即可。autostart:表示是否跟隨supervisor一起啟動。autorestart:如果該程式掛了,是否重新啟動。stdout_logfile:終端標準輸出重新導向檔案。stderr_logfile:終端錯誤輸出重新導向檔案。

啟動supervisor

[root@snails ~]# /usr/bin/supervisord -c /etc/supervisord.conf

如果出現什麼問題,可以查看日誌進行分析,記錄檔路徑/tmp/supervisord.log

tips:如果修改了設定檔,可以用kill -HUP重新載入設定檔

[root@snails ~]# cat /tmp/supervisord.pid | xargs sudo kill -HUP

查看supervisor運行狀態

[root@snails ~]# supervisorctlgolang-http-server               RUNNING   pid 30343, uptime 0:00:55

 * 輸入help可以查看協助

supervisor> helpdefault commands (type help <topic>):=====================================add    exit      open  reload  restart   start   tail   avail  fg        pid   remove  shutdown  status  update clear  maintail  quit  reread  signal    stop    version

supervisor運行原理

supervisor運行後本身是守護進程,通過自身來管理相應的子進程,通過觀察相應的進程狀態就很明了。

[root@snails ~]# ps -ef | grep supervisordroot     30269     1  0 11:31 ?        00:00:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf[root@snails ~]# ps -ef | grep simple_http_serverroot     30343 30269  0 11:45 ?        00:00:00 /root/simple_http_server

可以很直觀的看出golang simple_http_server進程是supervisord的子進程。

supervisor是否靠譜

supervisor的誕生已經10年了,現在是3+版本,所以放心使用吧。

升級Supervisor

升級Supervisor也是非常簡單的,使用 pip install --upgrade supervisor
既可以更新程式,然後使用 systemctl restart systemd-supervisor
重啟一下,就可以升級完成。

驗證

[root@snails ~]# systemctl enable systemd-supervisor[root@snails ~]# systemctl status systemd-supervisor[root@snails ~]# curl localhost:9090Hello world

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.