golang Development: Environment Section (f) Go run to monitor the use of Supervisord

Why use Supervisord

17 years of the first to write Go project, with development projects Go not much not much effort, and soon completed the development. When the test environment to deployment, because they do not know Supervisord this software, really took some effort. Can not follow the same development environment, directly execute binaries compiled it, even though the background, in case it hung up, no one knows, even if the testers found, the development had to log on to the server is started this binary file again. Obviously this solution does not make any sense, and later to find solutions online.
Then, Go consulting development former colleague, I found Supervisord, overjoyed. It is the optimal solution ah.

What Supervisord that?
Supervisord is a very useful process management tools implemented in Python, supervisord also requires management program is non-daemon program, supervisord will help you turn it into a daemon process, so if you use supervisord to manage nginx, it must be configured in the nginx file add a line to set daemon off let nginx way to start a non-daemon.
Program crashes or exit Supervisord will automatically start the program. So do not be afraid to go hang executable file or quit or die, Supervisord as long as the abnormal condition monitoring will restart the executable file and logging.

官方的解释
Supervisor: A Process Control System
Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.
It shares some of the same goals of programs like launchd, daemontools, and runit. Unlike some of these programs, it is not meant to be run as a substitute for init as “process id 1”. Instead it is meant to be used to control processes related to a project or a customer, and is meant to start like any other program at boot time.

It is the process control system. Monitoring and control processes on Unix systems.

Installation Supervisor

Of course, in our Supervisor is installed in a virtual machine

sudo vagrant ssh
apt-get install supervisor

supervisorctl -h
supervisorctl -- control applications run by supervisord from the cmd line.
Usage: /usr/bin/supervisorctl [options] [action [arguments]]
Options:
-c/--configuration FILENAME -- configuration file path (default /etc/supervisord.conf)
-h/--help -- print usage message and exit
出现上面的帮助信息表示安装成功了

安装完成后,可以查看配置
vim /etc/supervisor/supervisord.conf
看到
[include]
files = /etc/supervisor/conf.d/*.conf
supervisord 监控的项目的配置必须部署到 /etc/supervisor/conf.d/目录下,
并且使用conf后缀

For chestnuts

Go Web to find a simple project to try supervisor how to monitor software running.
Go first generates a compiled binary executable file
main.go

package main

import (
    "fmt"
    "net/http"
    "log"
)

func sayhelloName(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello China!")
}

func main() {
    http.HandleFunc("/", sayhelloName)
    err := http.ListenAndServe(":9090", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

Compiled executable file, test whether access is normal

go build -o test.gobin
./test.gobin
curl http://192.168.0.10:9090
Hello China!
说明Go Web是正常的

Next, we use the Go web Supervisord monitoring program.
Look at the software configuration instructions Supervisord monitoring of the
[program: project name]
the Command = / the Data / the WWW / Go / src / the Test / test.bin
startup command
autostart = true
also automatically activated when supervisord start
startsecs = 10
start no abnormal exit after 10 seconds, it means that the process starts normally, the default is 1 second
autorestart = true
program automatically restart after exit, optional value: [unexpected, true, false] , the default is unexpected, means that the process accidentally killed before restart
startretries = 3
failed to start automatically retries, the default is 3
the user = root
to start the process with which the user, the default is the root
priority = 999
process starts priority, default 999, the value of a small boot priority
redirect_stderr = true
to redirect stderr to stdout, defaults to false
stdout_logfile_maxbytes 20MB =
stdout log file size, default 50MB
stdout_logfile_backups = 20 is
stdout log file backup number, default is 10
stdout log file, you need to pay attention to when not start properly when the specified directory does not exist, it is necessary to create the directory manually (supervisord automatically creates a log file)
stdout_logfile = / the Data / logs / the Test / test.log
log output file address
stopasgroup = false
default is false, when the process is killed, whether to send a stop signal to the process group, including sub-processes
killasgroup = false
default is false, send a kill signal to the process group, including sub-processes

We generate executable file for the above test.bin create a Supervisord monitor configuration file, the configuration file must be in the directory /etc/supervisor/conf.d

cd  /etc/supervisor/conf.d
vim test.conf
[program:test]
command=/data/www/go/src/test/test.bin
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=/data/logs/test/test.log
stopasgroup=false
killasgroup=false

Run Supervisord, so that monitoring test.gobin

添加了新的配置之后一定需要执行
supervisorctl update
会提示
test: added process group
表示test添加成功了。
查看执行状态
supervisorctl status
test                             RUNNING   pid 4354, uptime 0:00:16

He said this operation was successful.
We try test.bin service request is normal.

curl http://192.168.0.10:9090
the Hello China!
The entire project monitoring services deployed.

Supervisord common commands

supervisorctl status 监控的程序的运行状态的列表
supervisorctl stop test 停止监控的程序
supervisorctl start test 启动监控的程序
supervisorctl restart test 重启监控的程序
supervisorctl update 更新监控的程序,如有新的配置添加一定要先执行update

To learn more, check out the official documentation of course.
http://supervisord.org/

Guess you like

Origin www.cnblogs.com/feixiangmanon/p/11067700.html