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守护进程编写和原理理解
如果该篇文章帮助了你,可以打赏作者哟
supervisor运行golang守护进程的更多相关文章
- Supervisor 管理后台守护进程
Supervisor 管理后台守护进程 参考原文如下: http://codinn.com/people/brant/notes/110948/ 做了一些注释 +++++++++++引用开始+++++ ...
- CentOS7 安装supervisor守护进程管理器
supervisor没有发布在标准的CentOS源在,需要安装epel源.这种方式安装的可能不是最新版本,但比较方便,安装完成之后,配置文件会自动帮你生成. 默认配置文件:/etc/superviso ...
- Supervisor(Linux/Unix进程管理工具)安装与配置
参考链接:https://blog.csdn.net/xyang81/article/details/51555473 Supervisor(http://supervisord.org/)是用Pyt ...
- supervisord守护进程的使用
原文链接:http://blog.csdn.net/xyang81/article/details/51555473 Supervisor(http://supervisord.org/)是用Pyth ...
- linux系统编程之进程(八):守护进程详解及创建,daemon()使用
一,守护进程概述 Linux Daemon(守护进程)是运行在后台的一种特殊进程.它独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.它不需要用户输入就能运行而且提供某种服务,不是对整个 ...
- 创建守护进程步骤与setsid() -- linux deamon进程
原创:http://www.cnblogs.com/mickole/p/3188321.html 一,守护进程概述 Linux Daemon(守护进程)是运行在后台的一种特殊进程.它独立于控制终端并且 ...
- Linux中的两种守护进程stand alone和xinetd
Linux中的两种守护进程stand alone和xinetd --http://www.cnblogs.com/itech/archive/2010/12/27/1914846.html#top 一 ...
- Linux守护进程详解(init.d和xinetd) [转]
一 Linux守护进程 Linux 服务器在启动时需要启动很多系统服务,它们向本地和网络用户提供了Linux的系统功能接口,直接面向应用程序和用户.提供这些服务的程序是由运行在后台 的守护进程来执行的 ...
- 深入理解Linux操作系统守护进程的意义
Linux服务器在启动时需要启动很多系统服务,它们向本地和网络用户提供了Linux的系统功能接口,直接面向应用程序和用户.提供这些服务的程序是由运行在后台的守护进程(daemons)来执行的.守护进程 ...
随机推荐
- SWPFILE实现(增加swap空间)
1.mkdir /var/swap chmod 700 /var/swap(可以不用设置) 2.dd if=/dev/zero of=/var/swap/file bs=1024 count=65 ...
- ODI 12c 安装
软件下载地址: http://www.oracle.com/technetwork/middleware/data-integrator/downloads/index.html 下载这个版本: Or ...
- Screen对象
document.write("Screen-width:"+screen.width+"Screen-height:"+screen.height);docu ...
- About Closure
Closure被翻译为闭包,C++11引入了Lambda表达式支持Closure,JavaScript支持Closure,Objective C支持Blocks,他们都是Closure,名字各有不同, ...
- 一维条形码攻击技术(Badbarcode)
0x00 前言 在日常生活中,条形码随处可见,特别在超市,便利店,物流业,但你们扫的条形码真的安全吗?之前TK教主 在PacSec介绍的条形码攻击和twitter上的demo视频太炫酷,所以就自己买了 ...
- php大力力 [018节]如何联系大力力
有事儿就注册博客园,给我发 博客园站内的 短消息呗,唉,没有人联系我呀,啦啦啦,爱我爱我,快点爱我 2015-08-26 php大力力018.如何联系大力力
- ie7下 滚动条内容不动问题
ie7+ 版式正常 ie7滚动内容不跟着动 解决方法 加上 overflow-x: hidden; overflow-y: auto; *position:relative; *le ...
- squid 延伸
#openssl req -new -x509 -days 365 -nodes -out stunnel.pem -keyout stunnel.pem # openssl gendh 512> ...
- C#实现union以及lock的使用
1.什么是Union类型数据 联合(Union)是一种特殊的类,一个联合中的数据成员在内存中的存储是互相重叠的.每个数据成员都在相同的内存地址开始. 分配给联合的存储区数量是“要包含它最大的数据成员” ...
- linux 将debug信息重定向到LCD(屏幕)
/*********************************************************************** * linux 将debug信息重定向到LCD(屏幕) ...