Kill doesn't kill

3324 views
Skip to first unread message

Peter Kleiweg

unread,
May 2, 2014, 1:42:02 PM5/2/14
to golan...@googlegroups.com
I have something like this:

    import "os/exec"

    cmd := exec.Command( some command )
    cmd.Start()
    err1 := cmd.Process.Kill() 
    err2 := cmd.Wait()

cmd.Wait() returns, err1 is nil, and err2 says something like "terminated", but the command is still running, and it keeps running until it is finished.

Am I missing something? All I want is to kill a process that was started with cmd.Start()

Chris Hines

unread,
May 2, 2014, 1:53:41 PM5/2/14
to golan...@googlegroups.com
What OS? It works for me on Windows, but the implementation is platform specific.

Peter Kleiweg

unread,
May 2, 2014, 2:38:08 PM5/2/14
to golan...@googlegroups.com


Op vrijdag 2 mei 2014 19:53:41 UTC+2 schreef Chris Hines:
OS is Linux.

It seems only the top process is killed, not its child processes 

I read somewhere you shouldn't kill the PID but the PGID, but when I do that, the Go program itself is killed too.

Tarrant Rollins

unread,
May 2, 2014, 2:42:01 PM5/2/14
to Peter Kleiweg, golan...@googlegroups.com
Linux has a flag you can set when calling a process that will cause it to kill its children when it is killed:
 
    cmd := exec.Command( some command )
    cmd.SysProcAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGKILL}
    cmd.Start()
 
This is a Linux only setting. My understanding is that it is generally better that  you create a new process group and killing that.
 
-T
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Peter Kleiweg

unread,
May 2, 2014, 3:22:16 PM5/2/14
to golan...@googlegroups.com, Peter Kleiweg
Op vrijdag 2 mei 2014 20:42:01 UTC+2 schreef Tarrant:

Linux has a flag you can set when calling a process that will cause it to kill its children when it is killed:
 
    cmd := exec.Command( some command )
    cmd.SysProcAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGKILL}
    cmd.Start()
 
This is a Linux only setting. My understanding is that it is generally better that  you create a new process group and killing that.

OK, this seems to do the trick with a separate process group:

    cmd := exec.Command( some_command )
    cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
    cmd.Start()

    pgid, err := syscall.Getpgid(cmd.Process.Pid)
    if err == nil {
        syscall.Kill(-pgid, 15)  // note the minus sign
    }

    cmd.Wait()

That's complicated. Why doesn't cmd.Kill() do this?

Brad Fitzpatrick

unread,
May 2, 2014, 3:31:51 PM5/2/14
to Peter Kleiweg, golang-nuts
How would cmd.Kill know what you want?



Dmitri Shuralyov

unread,
May 2, 2014, 10:17:46 PM5/2/14
to golan...@googlegroups.com, Peter Kleiweg
Killing a process doesn't kill the processes it started (i.e., it's children processes).

What's the command you're running?

If it's "go run main.go", for example, then keep in mind the actual process that runs is a child process.
Reply all
Reply to author
Forward
0 new messages