2019-01-02 20:50


  • signals

I'm having trouble sending a signal from a parent process and receiving it in the child process.

This is the code for the child process. It exits when it receives SIGINT.

// child.go
func main() {
    stop := make(chan os.Signal, 1)
    signal.Notify(stop, os.Interrupt)


This is the parent process. It starts child.go, sends SIGINT, then waits for it to exit.

// main.go
func main() {
    // Start child process
    cmd := exec.Command("go", "run", "child.go")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    err := cmd.Start()
    if err != nil {
        _, _ = fmt.Fprintf(os.Stderr, "Start: " + err.Error())

    // Wait, then send signal
    time.Sleep(time.Millisecond * 500)
    err = cmd.Process.Signal(os.Interrupt)
    if err != nil {
        _, _ = fmt.Fprintf(os.Stderr, "Signal: " + err.Error())

    // Wait for child process to finish
    err = cmd.Wait()
    if err != nil {
        _, _ = fmt.Fprintf(os.Stderr, "Wait: " + err.Error())

This code should print started stopped to show that it worked as expected, but it only prints started and hangs at cmd.Wait(), meaning the child process did not receive the signal.

When I run go run child.go it works fine, so I don't think the problem is with that file. I understand that func (*Process) Signal doesn't work on Windows; I am using Linux.

How can I fix the code so that the child process gets the signal sent by the parent process?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享