我正在编写一个小程序来管理重启到其他进程。golang重启父进程没有收到SIGINT
基本上,当一个应用程序进程启动(称为A)时,它会产生一个新的进程(称为D),它具有一个简单的HTTP服务器。当D接收到一个http请求时,它杀死A并重新启动它。
问题是,现在不响应CTRL-C,我不知道为什么。这可能是一些简单的事情,也可能我并不真正了解进程,终端和信号之间的关系。但它使用相同的stdin/stdout/stderr在同一终端中运行。以下是展示此行为的完整程序。
package main
import (
"flag"
"log"
"net/http"
"os"
"os/exec"
"strconv"
"time"
)
/*
Running this program starts an app (repeatdly prints 'hi') and spawns a new process running a simple HTTP server
When the server receives a request, it kills the other process and restarts it.
All three processes use the same stdin/stdout/stderr.
The restarted process does not respond to CTRL-C :(
*/
var serv = flag.Bool("serv", false, "run server")
// run the app or run the server
func main() {
flag.Parse()
if *serv {
runServer()
} else {
runApp()
}
}
// handle request to server
// url should contain pid of process to restart
func handler(w http.ResponseWriter, r *http.Request) {
pid, err := strconv.Atoi(r.URL.Path[1:])
if err != nil {
log.Println("send a number...")
}
// find the process
proc, err := os.FindProcess(pid)
if err != nil {
log.Println("can't find proc", pid)
return
}
// terminate the process
log.Println("Terminating the process...")
err = proc.Signal(os.Interrupt)
if err != nil {
log.Println("failed to signal interupt")
return
}
// restart the process
cmd := exec.Command("restarter")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
log.Println("Failed to restart app")
return
}
log.Println("Process restarted")
}
// run the server.
// this will only work the first time and that's fine
func runServer() {
http.HandleFunc("/", handler)
if err := http.ListenAndServe(":9999", nil); err != nil {
log.Println(err)
}
}
// the app prints 'hi' in a loop
// but first it spawns a child process which runs the server
func runApp() {
cmd := exec.Command("restarter", "-serv")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
log.Println(err)
}
log.Println("This is my process. It goes like this")
log.Println("PID:", os.Getpid())
for {
time.Sleep(time.Second)
log.Println("hi again")
}
}
该程序预计将被安装。为方便起见,您可以使用go get github.com/ebuchman/restarter
来获取。
使用restarter
运行该程序。它应该打印它的进程ID。然后curl http://localhost:9999/<procid>
启动重启。新进程现在不会响应CTRL-C。为什么?我错过了什么?
不幸的是,这并没有帮助。问题不在于该过程没有处理信号的处理程序,而是信号根本没有进入该过程,因为它处于由其孩子重新启动的奇怪状态。这就像终端已经失去了过程的钩子,但pgid的没有改变。 – Ethan 2015-02-08 20:35:41