作为一名 php 开发老兵。使用过命令行对 nginx、php-fpm 进行启动/重启/停止等操作。印象非常深刻。让我用 c/c++ 开发这样的系统肯定是没精力搞了。然而,自从 golang 进入了我的视野之后。我发现这一切都变得非常的容易。
1)生成守护进程直接上代码:
package mainimport ( os os/exec path/filepath)func main() { //判 断当其是否是子进程,当父进程return之后,子进程会被 系统1 号进程接管 if os.getppid() != 1 { // 将命令行参数中执行文件路径转换成可用路径 filepath, _ := filepath.abs(os.args[0]) cmd := exec.command(filepath, os.args[1:]...) // 将其他命令传入生成出的进程 cmd.stdin = os.stdin // 给新进程设置文件描述符,可以重定向到文件中 cmd.stdout = os.stdout cmd.stderr = os.stderr cmd.start() // 开始执行新进程,不等待新进程退出 return }}
对 linux 系统熟悉的人应该知道:用户创建的守护进程会被 linux 系统的 1 号进程接管。换句话说,上面的代码只能在 linux 系统运行。unix 系统我没有玩过。所以,也不能给出具体的建议。
我在网上看到还有其他的方法实现守护进程的创建。但是,我觉得只有上面源码的方式我觉得不错。并且成功用于项目当中。
比如:
os.startprocess() 创建守护进程。syscall.rawsyscall() 创建守护进程。
唯独 exec.command 创建守护进程的方式最高级。封装得最好。推荐使用这种试。
2) 守护进程启动/重启/停止在第 1 点当中,我们已经成功启动了一个守护进程。但是,我们不可能使用 kill 命令去结束它。然后,再启动吧。所以,我们要用业界专业的手法:信号。
任何进程在运行中都能接收到我们发送给它的信号。关于 linux 的信号有很多。大家可以自己 google 搜索关键词:linux 信号。
直接上源码:
package mainimport fmtimport osimport os/signalimport syscallfunc main() { // go signal notification works by sending `os.signal` // values on a channel. we'll create a channel to // receive these notifications (we'll also make one to // notify us when the program can exit). sigs := make(chan os.signal, 1) done := make(chan bool, 1) // `signal.notify` registers the given channel to // receive notifications of the specified signals. signal.notify(sigs, syscall.sigint, syscall.sigterm) // this goroutine executes a blocking receive for // signals. when it gets one it'll print it out // and then notify the program that it can finish. go func() { sig := <-sigs fmt.println() fmt.println(sig) done <- true }() // the program will wait here until it gets the // expected signal (as indicated by the goroutine // above sending a value on `done`) and then exit. fmt.println(awaiting signal) <-done fmt.println(exiting)}
有三个关键点:
1)注册信号
2)接收信号
3)处理信号。
只要把创建守护进程与信号量处理整合一起,就能实现命令去管理守护进程了。
更多golang相关技术文章,请访问golang教程栏目!
以上就是解析golang怎么创建守护进程和平滑重启的详细内容。