zoukankan      html  css  js  c++  java
  • supervisor运行golang守护进程

    最近在鼓捣golang守护进程的实现,无意发现了supervisor这个有意思的东西。supervisor是一个unix的系统进程管理软件,可以用它来管理apache、nginx等服务,若服务挂了可以让它们自动重启。当然也可以用来实现golang的守护进程,下面描述下具体实现。

    安装supervisor

    基于centos 6.4。

    supervisor使用python编写的,可以用easy_install安装。centos上默认有python的运行环境,安装起来就非常简单了。

    $ sudo yum install python-setuptools
    $ sudo easy_install supervisor

    如果没有看到什么报错,那么就安装成功了,可以使用echo_supervisord_conf查看配置详情,而后生成配置文件。

    $ sudo echo_supervisord_conf > /etc/supervisord.conf

    golang http服务

    先整一个简单的golang http服务

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    package main
     
    import (
        "fmt"
        "log"
        "net/http"
    )
     
    func main() {
        http.HandleFunc("/"func(w http.ResponseWriter, r *http.Request) {
            fmt.Fprintf(w, "Hello world")
        })
     
        err := http.ListenAndServe(":9090", nil)
        if err != nil {
            log.Fatal("ListenAndServe: ", err)
        }
    }

    直接运行这个程序会占用住终端,下面看看如何用supervisor来跑这个程序。

    supervisor配置golang

    编辑/etc/supervisord.conf,在最后增加运行程序设置

    [program:golang-http-server]
    command=/home/golang/simple_http_server
    autostart=true
    autorestart=true
    startsecs=10
    stdout_logfile=/var/log/simple_http_server.log
    stdout_logfile_maxbytes=1MB
    stdout_logfile_backups=10
    stdout_capture_maxbytes=1MB
    stderr_logfile=/var/log/simple_http_server.log
    stderr_logfile_maxbytes=1MB
    stderr_logfile_backups=10
    stderr_capture_maxbytes=1MB
    

    几个配置说明:

    command:表示运行的命令,填入完整的路径即可。
    autostart:表示是否跟随supervisor一起启动。
    autorestart:如果该程序挂了,是否重新启动。
    stdout_logfile:终端标准输出重定向文件。
    stderr_logfile:终端错误输出重定向文件。

    其余配置说明可以查看官方文档。

    启动supervisor

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

    如果出现什么问题,可以查看日志进行分析,日志文件路径/tmp/supervisord.log

    tips:如果修改了配置文件,可以用kill -HUP重新加载配置文件

    $ cat /tmp/supervisord.pid | xargs sudo kill -HUP

    查看supervisor运行状态

    $ supervisorctl
    golang-http-server RUNNING pid 23307, uptime 0:02:55
    supervisor>

    输入help可以查看帮助

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

    supervisor运行原理

    supervisor运行后本身是守护进程,通过自身来管理相应的子进程,通过观察相应的进程状态就很明了了。

    $ ps -ef | grep supervisord
    root 23306 1 0 07:30 ? 00:00:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
    root 23331 23222 0 07:41 pts/0 00:00:00 grep supervisord
    
    $ ps -ef | grep simple_http_server
    root 23307 23306 0 07:30 ? 00:00:00 /home/golang/simple_http_server
    root 23333 23222 0 07:41 pts/0 00:00:00 grep simple_http_server

    可以很直观的看出golang simple_http_server进程是supervisord的子进程。

    supervisor是否靠谱

    supervisor的诞生已经10年了,现在是3+版本,所以放心使用吧。

    参考

    supervisor官网:http://supervisord.org/

    关于守护进程以前在玩python的时候有写过其中实现的原理,详细可以参考:linux下python守护进程编写和原理理解

    如果该篇文章帮助了你,可以打赏作者哟
  • 相关阅读:
    不常用的cmd命令
    js获取宽度
    Marshaling Data with Platform Invoke 概览
    Calling a DLL Function 之三 How to: Implement Callback Functions
    Marshaling Data with Platform Invoke 之四 Marshaling Arrays of Types
    Marshaling Data with Platform Invoke 之一 Platform Invoke Data Types
    Marshaling Data with Platform Invoke 之三 Marshaling Classes, Structures, and Unions(用时查阅)
    Calling a DLL Function 之二 Callback Functions
    WCF 引论
    Marshaling Data with Platform Invoke 之二 Marshaling Strings (用时查阅)
  • 原文地址:https://www.cnblogs.com/cnsanshao/p/4652164.html
Copyright © 2011-2022 走看看