【问题标题】:golang restarted parent process doesn't receive SIGINTgolang重新启动的父进程没有收到SIGINT
【发布时间】: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


    【解决方案1】:

    您可以查看两个 http 服务器框架采用的方法来监听和拦截信号(包括SIGINT,甚至是SIGTERM

    【讨论】:

    • 不幸的是,这没有帮助。问题不在于进程没有信号处理程序,而是信号根本没有到达进程,因为它处于被其子进程重新启动的奇怪状态。就像终端失去了对进程的钩子,但 pgid 没有改变。
    【解决方案2】:

    这与 Go 没有任何关系。您从终端 shell 启动进程 A。进程 A 启动进程 D(不确定 B 发生了什么,但没关系)。进程 D 杀死进程 A。现在你的 shell 看到它启动的进程已经退出,所以 shell 准备监听另一个命令。进程 D 启动进程 A 的另一个副本,但 shell 对此一无所知。当您键入 ^C 时,shell 将处理它。如果您运行另一个程序,shell 将安排 ^C 转到该程序。 shell 对进程 A 的副本一无所知,因此它永远不会将 ^C 指向该进程。

    【讨论】:

    • 是的。我试图了解即使父母死了,是否可以让终端连接到进程组。它不是。所以解决方案是让A启动A',让D杀死并重新启动A',然后让A阻塞,这样终端就可以向整个组发出信号
    最近更新 更多