Recently in tinkering with the implementation of the Golang daemon, I have no intention of discovering supervisor this interesting thing. Supervisor is a UNIX system process management software that can be used to manage Apache, Nginx and other services, if the service hangs can let them automatically restart. Of course, can also be used to implement the Golang daemon, the following describes the implementation of the specific.
Installing Supervisor
Based on CentOS 6.4.
Supervisor is written in Python and can be installed using Easy_install. The Python runtime is the default on CentOS, and it's easy to install.
$ sudo yum install python-setuptools$ sudo easy_install supervisor
If you do not see any error, then the installation is successful, you can use echo_supervisord_conf to view configuration details, and then generate the configuration file.
$ sudo echo_supervisord_conf >/etc/supervisord.conf
Golang HTTP Service
First, a simple Golang HTTP service
123456789101112131415161718 |
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)
}
}
|
Run this program directly will occupy the terminal, below to see how to use Supervisor to run this program.
Supervisor Configuration Golang
Edit/etc/supervisord.conf, add run program settings at the end
[program:golang-http-server]command=/home/golang/simple_http_serverautostart=trueautorestart=truestartsecs= 10stdout_logfile=/var/log/simple_http_server.logstdout_logfile_maxbytes=1mbstdout_logfile_backups=10stdout_ Capture_maxbytes=1mbstderr_logfile=/var/log/simple_http_server.logstderr_logfile_maxbytes=1mbstderr_logfile_ Backups=10stderr_capture_maxbytes=1mb
Several configuration instructions:
Command: Indicates the running commands, fill in the full path.
Autostart: Indicates whether to start with supervisor.
AutoRestart: If the program is hung, restart it.
Stdout_logfile: Terminal standard output redirection file.
Stderr_logfile: Terminal error output redirect file.
The remaining configuration instructions can be viewed in the official documentation.
Start Supervisor
$ sudo/usr/bin/supervisord-c/etc/supervisord.conf
If there is any problem, you can view the log for analysis, log file path/tmp/supervisord.log
Tips: If you modify a configuration file, you can reload the configuration file with Kill-hup
$ cat/tmp/supervisord.pid | Xargs sudo kill-hup
View Supervisor Run Status
$ supervisorctlgolang-http-server RUNNING pid 23307, uptime 0:02:55supervisor>
Enter Help to view assistance
supervisor> helpdefault commands (Type Help): =====================================add clear FG Open quit Remove Resta RT start Stop Updateavail exit Maintail pid Reload reread shutdown status tail version
Supervisor Operating principle
Supervisor after running itself is the daemon, through itself to manage the corresponding sub-process, by observing the corresponding process state is very clear.
$ PS-EF | grep supervisordroot 23306 1 0 07:30? 00:00:00/usr/bin/python/usr/bin/supervisord-c/etc/supervisord.confroot 23331 23222 0 07:41 pts/0 00:00:00 grep Supervi sord$ Ps-ef | grep simple_http_serverroot 23307 23306 0 07:30? 00:00:00/home/golang/simple_http_serverroot 23333 23222 0 07:41 pts/0 00:00:00 grep simple_http_server
It is very intuitive to see that the Golang simple_http_server process is a supervisord child process.
Whether the supervisor is reliable
Supervisor has been born for 10 years, and now it is an old version, so use it with ease.
Reference
Supervisor Official Website: http://supervisord.org/
About the daemon in the past when playing Python has written the implementation of the principle, detailed can be consulted: Linux under the process of writing and understanding of the principle of Python
If the article helped you, you can play the author yo
Supervisor running the Golang daemon