最近在鼓捣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守护进程编写和原理理解

如果该篇文章帮助了你,可以打赏作者哟

supervisor运行golang守护进程的更多相关文章

  1. Supervisor 管理后台守护进程

    Supervisor 管理后台守护进程 参考原文如下: http://codinn.com/people/brant/notes/110948/ 做了一些注释 +++++++++++引用开始+++++ ...

  2. CentOS7 安装supervisor守护进程管理器

    supervisor没有发布在标准的CentOS源在,需要安装epel源.这种方式安装的可能不是最新版本,但比较方便,安装完成之后,配置文件会自动帮你生成. 默认配置文件:/etc/superviso ...

  3. Supervisor(Linux/Unix进程管理工具)安装与配置

    参考链接:https://blog.csdn.net/xyang81/article/details/51555473 Supervisor(http://supervisord.org/)是用Pyt ...

  4. supervisord守护进程的使用

    原文链接:http://blog.csdn.net/xyang81/article/details/51555473 Supervisor(http://supervisord.org/)是用Pyth ...

  5. linux系统编程之进程(八):守护进程详解及创建,daemon()使用

    一,守护进程概述 Linux Daemon(守护进程)是运行在后台的一种特殊进程.它独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.它不需要用户输入就能运行而且提供某种服务,不是对整个 ...

  6. 创建守护进程步骤与setsid() -- linux deamon进程

    原创:http://www.cnblogs.com/mickole/p/3188321.html 一,守护进程概述 Linux Daemon(守护进程)是运行在后台的一种特殊进程.它独立于控制终端并且 ...

  7. Linux中的两种守护进程stand alone和xinetd

    Linux中的两种守护进程stand alone和xinetd --http://www.cnblogs.com/itech/archive/2010/12/27/1914846.html#top 一 ...

  8. Linux守护进程详解(init.d和xinetd) [转]

    一 Linux守护进程 Linux 服务器在启动时需要启动很多系统服务,它们向本地和网络用户提供了Linux的系统功能接口,直接面向应用程序和用户.提供这些服务的程序是由运行在后台 的守护进程来执行的 ...

  9. 深入理解Linux操作系统守护进程的意义

    Linux服务器在启动时需要启动很多系统服务,它们向本地和网络用户提供了Linux的系统功能接口,直接面向应用程序和用户.提供这些服务的程序是由运行在后台的守护进程(daemons)来执行的.守护进程 ...

随机推荐

  1. table td的宽度详解

    前言:一直总觉得td的宽度好难驾驭,但万事万物总是有规律的.就像亮剑说的:不用因为怕八路就敬而远之,应该靠上去,熟悉他们,了解他们.   正文:           Table只有Table的宽度是可 ...

  2. Android异步更新UI的四种方式

    Android异步更新UI的四种方式 2015-09-06 09:23 segmentfault 字号:T | T 大家都知道由于性能要求,android要求只能在UI线程中更新UI,要想在其他线程中 ...

  3. hdu 2081

    PS:...找到好多水题.... #include "stdio.h" int main(){ ]; int i,j,n,N; scanf("%d",& ...

  4. 技术分享:逆向分析ATM分离器

    文章内容仅供技术交流,请勿模仿操作! 背景(作者) 每一次外出时, Elizabeth和我总是格外的小心,同时把我们身上的钱藏在特殊的皮带上面,这样还不够,我们还采取了“狡兔三窟”的方式来藏身上带的银 ...

  5. gen already exists but is not a source folder ZT

    解决方法:1. 右键点击工程,选择 "Properties"2. 选择左边的 "Java Build Path" 3. 打开 "Source" ...

  6. MongoDB索引、聚合

    用$where可以执行任意的js作为查询的一部分. db.foo.find({"$where" : function(){          for(var current in ...

  7. 自定义NavigationBar

    [self.navigationController.navigationBarsetHidden:YES]; CGRect screenRect = [[UIScreen mainScreen] b ...

  8. 设置dt height 保证dd在同一行

    <html>   <head>   <meta charset="UTF-8">   <meta name="Author&qu ...

  9. 在Entity Framework中使用事务

    继续为想使用Entity Framework的朋友在前面探路,分享的东西虽然技术含量不高,但都是经过实践检验的. 在Entity Framework中使用事务很简单,将操作放在TransactionS ...

  10. Linux systemd 打开调试终端、添加开机自运行程序

    /************************************************************************* * Linux systemd 打开调试终端.添加 ...