一、介绍Supervisord软件
1、什么是Supervisord?
Supervisord是一个非常优秀的进程管理工具,使用Python开发。它可以在类UNIX系统的方式让用户来准确地监视和控制后台一定数量的服务进程。并作为一个天使进程让后台进程在当发生内部错误退出、或者进程被意外杀死时自动重启。除此之外,supervisord可以监控TCP端口,让其他主机通过客户端了命令supervisorctl通过HTTP协议直接对Server端进程进行启停,避免让进程/服务器管理者直接接触Shell或root用户。进程之间也有一个优先级和进程组关系,让管理员使用start all和stop all的关系来启动。

2、安装Supervisord软件

[root@yyljxl ~]# cat /etc/redhat-release
CentOS Linux release 7.x

[root@www ]# yum -y install supervisor
[root@yyljxl ~]# supervisorctl -h
supervisorctl -- control applications run by supervisord from the cmd line.

Usage: /usr/bin/supervisorctl [options] [action [arguments]]

Options:
-c/--configuration -- configuration file path (default /etc/supervisord.conf)
-h/--help -- print usage message and exit
-i/--interactive -- start an interactive shell after executing commands
-s/--serverurl URL -- URL on which supervisord server is listening
(default "http://localhost:9001").
-u/--username -- username to use for authentication with server
-p/--password -- password to use for authentication with server
-r/--history-file -- keep a readline history (if readline is available)

action [arguments] -- see below

Actions are commands like "tail" or "stop". If -i is specified or no action is
specified on the command line, a "shell" interpreting actions typed
interactively is started. Use the action "help" to find out about available
actions.
出现上面的帮助信息表示安装成功了。

#启动服务
systemctl start supervisord
systemctl enable supervisord
systemctl status supervisord

#supervisord监控的软件的配置说明
vim /etc/supervisor/conf.d
[program:项目名]
command=/data/www/go/src/test/test.bin
程序启动命令
autostart=true
在supervisord启动的时候也自动启动
startsecs=10
启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
autorestart=true
程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
startretries=3
启动失败自动重试次数,默认是3
user=root
用哪个用户启动进程,默认是root
priority=999
进程启动优先级,默认999,值小的优先启动
redirect_stderr=true
把stderr重定向到stdout,默认false
stdout_logfile_maxbytes=20MB
stdout 日志文件大小,默认50MB
stdout_logfile_backups = 20
stdout 日志文件备份数,默认是10
stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
stdout_logfile=/data/logs/test/test.log
日志输出的文件地址
stopasgroup=false
默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=false
默认为false,向进程组发送kill信号,包括子进程。

#官方文档
http://supervisord.org/

3、使用案例
示例:编写一个简单的go web项目
[root@web scripts]# cat mail.go
package main

import (
"fmt"
"net/http"
)

//w, 给客户端回复数据
//r, 读取客户端发送的数据
func HandConn(w http.ResponseWriter, r *http.Request) {
fmt.Println("r.Method = ", r.Method)
fmt.Println("r.URL = ", r.URL)
fmt.Println("r.Header = ", r.Header)
fmt.Println("r.Body = ", r.Body)

w.Write([]byte("hello go")) //给客户端回复数据
}

func main() {
//注册处理函数,用户连接,自动调用指定的处理函数
http.HandleFunc("/", HandConn)

//监听绑定
http.ListenAndServe(":8000", nil)
}

#上传程序到Centos7.x系统
yum install go -y

#build程序
[root@yyljxl scripts]# go build -o test

#生成可执行文件
[root@yyljxl scripts]# ll
total 6416
-rw-r--r-- 1 root root 542 Jun 24 09:15 mail.go
-rwxr-xr-x 1 root root 6562362 Jun 24 09:37 test

#运行程序
[root@yyljxl scripts]# nohup ./test &
[1] 14132
[root@yyljxl scripts]# nohup: ignoring input and appending output to ‘nohup.out’

#查看进程
[root@yyljxl ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp6 0 0 :::8000 :::* LISTEN 14132/ ./test

4、配置test程序用supervisord监控
[root@www scripts]#
mkdir -p /nulige/scripts/logs
touch /nulige/scripts/logs/test.log
#配置supervisor监控test
cat > /etc/supervisord.d/test.ini <<EOF
[program:test]
command=/nulige/scripts/test
autostart=true
startsecs=10
autorestart=true
startretries=3
user=root
priority=999
redirect_stderr=true
stdout_logfile_maxbytes=20MB
stdout_logfile_backups = 20
stdout_logfile=/nulige/scripts/logs/test.log
stopasgroup=false
killasgroup=false
EOF

#执行命令
[root@yyljxl scripts]# supervisorctl update
test: added process group
[root@yyljxl scripts]# supervisorctl status
test RUNNING pid 15214, uptime 0:00:09

#端口占用,导致没有载入成功,kill掉原端口程序,再重新载入
[root@yyljxl scripts]# supervisorctl status
test FATAL Exited too quickly (process log may have details)

#重新载入
[root@yyljxl scripts]# supervisorctl reload
Restarted supervisord

#更新监控的程序
[root@yyljxl scripts]# supervisorctl update

#查看运行状态
[root@yyljxl scripts]# supervisorctl status
test RUNNING pid 15214, uptime 0:00:09

#停止服务
[root@yyljxl scripts]# supervisorctl stop test
test: stopped

#测试杀当前程序的pid,再观察pid变化。

#查看进程
[root@yyljxl ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp6 0 0 :::8000 :::* LISTEN 15385/test #杀进程
[root@yyljxl ~]# kill 15385
[root@yyljxl ~]# kill 15385
-bash: kill: (15385) - No such process #查看进程,发现PID已变化,说明监控成功,服务停止后,会自启动。
[root@yyljxl ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp6 0 0 :::8000 :::* LISTEN 15416/test

5、supervisord常用命令

supervisorctl status #监控的程序的运行状态的列表
supervisorctl stop test #停止监控的程序
supervisorctl start test #启动监控的程序
supervisorctl restart test #重启监控的程序
supervisorctl update #更新监控的程序,如有新的配置添加一定要先执行update
supervisorctl reload #载入最新的配置文件,并按新的配置启动、管理所有进程。

使用Supervisord软件管理go服务进程的更多相关文章

  1. linux软件管理之------编译安装nginx服务器并手动编写自动化运行脚本

    红帽系列的 linux软件管理分为三类:1. rpm 安装软件.2. yum 安装软件.3. 源码包编译安装.前面两种会在相关专题给出详细讲解.源码包的编译安装是非常关键的,我们知道linux的相关版 ...

  2. XC软件管理器应用

    这是一个基于android 4.4开发的android应用-XC软件管理器.包含应用的信息查看,打开应用以及应用的卸载等功能.非常实用的一个应用,欢迎大家下载使用. 下载地址:http://downl ...

  3. IT软件管理人员的职业路线(从技术经理到总经理) - CEO之公司管理经验谈

    技术.业务和管理永远是工作的一个话题.笔者今天就根据自身的经验,通过这三个方面介绍下IT软件管理人员的职业路线.前面写过一个文(IT软件技术人员的职位路线(从程序员到技术总监) - 部门管理经验谈), ...

  4. Debian系列软件管理(第二版)

    Debian系列软件管理 1.搜索软件包信息 apt-cache search apt-cache search yum 2.查看软件包信息 apt-cache show rpm 3.安装软件 apt ...

  5. RedHat系列软件管理(第二版) --脚本安装

    RedHat系列软件管理 --脚本安装 一.解压缩 tar -zxvf webmin-1.700.tar.gz 二.进入相关目录 cd webmin-1.700 三.如果此时执行./configure ...

  6. RedHat系列软件管理(第二版) --源码包安装

    RedHat系列软件管理 --源码包安装 源码包特点: 拥有广泛的平台支持性,可以装在所有的类UNIX操作系统上,不用考虑CPU架构. 灵活性,可以在安装过程中指定特有的选项. 定制度非常高,可以自己 ...

  7. RedHat系列软件管理(第二版) --二进制软件包管理

    RedHat系列软件管理 --二进制软件包管理 Linux学习思想-Linux相对与Windows来非常透明,因此,无论是系统,还是软件,都会有本身自带,或者是Man给提供的非常详细的说明/帮助文档, ...

  8. 末学者笔记--rpm和yum软件管理(Linux)

    一.rpm简介 这是一个数据库管理工具,可以通过读取数据库,判断软件是否已经安装,如果已经安装可以读取出来所有文件的所在位置等,并可以实现删除这些文件. rpm:RPM is Redhat Packa ...

  9. rpm和yum软件管理

    rpm简介 数据库管理工具,通过读取数据库,判断软件是否安装,如已安装可读取出来所有文件的所在位置等,并可以实现删除这些文件. rpm:RPM is Redhat Package Manager(递归 ...

随机推荐

  1. VMware Xcode真机调试

    原因如下:VMware12默认使用usb3.0 ,先给苹果系统关机,然后打开虚拟机设置,更改usb控制器为USB2.0 就可以成功连接了. 问题提示:could not launch “name” p ...

  2. JDK1.8 LocalDate 使用方式;LocalDate 封装Util,LocalDate工具类(一)

    未完待续 ........ 由于SimpleDateFormat存在线程安全问题,所以在JDK1.8中使用LocalDate和LocalDateTime来进行日期的工具类使用,下边就是原创的Local ...

  3. MySQL 学习网站

    [大型网站] CSDN论坛MySQL版块 https://bbs.csdn.net/forums/MySQL MySlide搜索MySQL https://myslide.cn/slides/1758 ...

  4. KITTI数据集

    目的 使用雷达点云提供的深度信息 如何实现 将雷达的三维点云投影到相机的二维图像上 kitti数据集简介 kitti的数据采集平台,配置有四个摄像机和一个激光雷达,四个摄像机中有两个灰度摄像机,两个彩 ...

  5. 五个goland进行go开发的小技巧

    五个goland进行go开发的小技巧 本文译自5 Tips To Speed Up Golang Development With IntelliJ Or Goland 确实很实用. 1. 实现int ...

  6. Java的修饰、继承、接口、抽象类

     1.private 修饰属性或者方法,只能在本类中被访问,定义后需要加get()set()方法,这样提高数据的安全性 私有属性虽然不能直接访问,但是其对象 或者 子类对象可以通过公有方法进行设值和获 ...

  7. #51nod上topcoder练习记

    好久没刷51nod了,又听说topcoder有很多好题.那么就来51nod上刷吧.(那个客户端搞得有点烦(看不懂)) [1366 贫富差距] 当图不连通的时候,答案为无穷大. 当图连通时,两个点之间的 ...

  8. 最全BT磁力搜索引擎索引(整理分享,不断更新...)

    最全BT磁力搜索引擎索引(整理分享,不断更新...) btkitty:http://cnbtkitty.com/(知名的BT磁力搜索,资源很多) idope.se:https://idope.se/( ...

  9. Native Clojure with GraalVM

    转自:https://www.innoq.com/en/blog/native-clojure-and-graalvm/ GraalVM is a fascinating piece of techn ...

  10. C++ CGI报“资源访问错误”问题分析

    一线上CGI偶发性会报“资源访问错误”,经过分析得出是因为CgiHost没有读取到CGI的任务输出,即CGI运行完成后连HTTP头都没有一点输出. 然而实际上,不可能没有任何输出,因为CGI至少有无条 ...