Go program fail to interrupt process

Hi,
I’m running go version go1.9.3 darwin/amd64.
The code :

cmd := exec.Command(command, options.Args...) // bash process - ruby running forever
cmd.Start()
go cmd.Wait() // I can see my process up and get to it
time.Sleep(20 * time.Second)
syscall.Kill(cmd.Process.Pid, syscall.SIGINT) // ruby process get the signal and do nothing with it
time.Sleep(5 * time.Second)
cmd.Process.Signal(os.Interrupt) // ruby process get the signal and do nothing with it
time.Sleep(5 * time.Second)
syscall.Kill(p.cmd.Process.Pid, syscall.SIGTERM) // ruby process get the signal and do nothing with it
time.Sleep(5 * time.Second)
syscall.Kill(p.cmd.Process.Pid, syscall.SIGKILL) //This will kill the process
//cmd.Process.Kill() //This will kill the process
//cmd.Process.Signal(os.Kill) //This will kill the process

The problem is that the only way to stop the process is killing it immediately (equal to kill -9 )
Manually it is possible to interrupt or terminate the process and not to kill it.

I would like to understand how to interrupt the process in a way it will work

What to you mean by manual kill? Ctrl-C or “kill pid”?

manually kill -2 is working fine

if err := cmd.Process.Kill(); err != nil {
    log.Fatal("failed to kill: ", err)
}

There is also Signal method on Process.

https://golang.org/src/os/exec.go?s=3842:3884#L110

syscall.Kill also returns an error, check it.

This kill is working, but its equals to kill -9 and not to interrupt.
I would like to release the process in a softer way.

Just use cmd.Process.Signal(os.Interrupt)

tried, the process get the signal and the process not stop

But that’s up to the process receiving the signal.

Yes, it must be the process then, thy some other process which handles signals for sure.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.