最近在鼓捣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. Format a Hard Drive in Csharp C#格式化总结

    using System; using System.Diagnostics; using System.IO; using System.Linq; using System.Management; ...

  2. JDBC体会

    1.把mysql-connector XXXX版本导入buildpath 2.通过DriverManager 的getConnection 方法获得一个Connnection引用,方法的参数是 url ...

  3. hdu 1069

    //Accepted 264 KB 0 ms //每种block只有三种方法,且每种放法至多放一次 //规定三条边的顺序后 //把所有的block按x递增排序,x相同则按y递增排序 //然后dp // ...

  4. Office word excel电子表格在线编辑的实现方法

    Office xp之后的版本支持通过webdav协议(http的扩展)直接编辑服务器上的文件. IIS(6.0)支持webdav,这在IIS管理器的web服务扩展中可以看到.利用IIS作为webdav ...

  5. sublime3笔记

    选择类Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本. Alt+F3 选中文本按下快捷键,即可一次性选择全部的相同文本进行同时编辑.举个栗子:快速选中并更改所有相同的变量名.函数名 ...

  6. vijos 1780 开车旅行

    细节巨多. 倍增即可. #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

  7. JQuery源码分析(三)

    jQuery中ready与load事件 jQuery有3种针对文档加载的方法 $(document).ready(function() { // ...代码... }) //document read ...

  8. iOS字体设置

    label.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:24]; 字体名如下: Font Family: Amer ...

  9. IPhone手机自动添加到itunes设置

    一,项目设置 如图:点击项目--info 在key下面条目上右键点击,选择添加Application supports iTunes file sharing   value设置为yes

  10. HDU 3932

    http://acm.hdu.edu.cn/showproblem.php?pid=3932 一定范围的平面上给一些点,求到这些点的最大距离最小,和上一题的题意正好相反,稍微改一下就可以 这个问题又叫 ...