【发布时间】:2015-04-07 22:51:14
【问题描述】:
我正在编写一个小程序来管理其他进程的重启。
基本上,当一个应用程序进程启动时(称为 A),它会产生一个新进程(称为 D),它有一个简单的 HTTP 服务器。当 D 收到一个 http 请求时,它会杀死 A 并重新启动它。
问题是,A 现在不响应 CTRL-C,我不知道为什么。可能很简单,也可能我不太了解进程、终端和信号之间的关系。但它运行在具有相同标准输入/标准输出/标准错误的同一终端中。以下是演示此行为的完整程序。
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。为什么?我错过了什么?
【问题讨论】:
标签: process go child-process kill-process sigint