# Killing child process on timeout in Go code

**URL:** <https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995>\
**Category:** Getting Help\
**Created:** [October 3, 2015, 3:39am UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995 "2015-10-03T03:39:27Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![VarunKSaini](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/varunksaini/32/251_2.png) [@VarunKSaini](https://forum.golangbridge.org/u/VarunKSaini)\
**Post date:** [October 3, 2015, 3:39am UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995/1 "2015-10-03T03:39:27Z")

</div>

I have a situations where I need to kill a process after some time. I start the process and then

```
case <-time.After(timeout):
		if err := cmd.Process.Kill(); err != nil {
			return 0, fmt.Errorf("Failed to kill process: %v", err)
		}

```

kills the process. But it only kills the parent process not the 5-10 child processes that main process starts. I also tried creating a process group and then doing

```
syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)

```

to kill all the process, but not working. Need help from experts now.

---

<div class="post-metadata">

**Author:** ![Stabby](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/stabby/32/55_2.png) [@Stabby](https://forum.golangbridge.org/u/Stabby)\
**Post date:** [October 3, 2015, 12:36pm UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995/2 "2015-10-03T12:36:03Z")

</div>

Can you not trap the kill signal in the parent process, and have it reap it’s children, before terminating itself?

---

<div class="post-metadata">

**Author:** ![dlclark](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/dlclark/32/785_2.png) [@dlclark](https://forum.golangbridge.org/u/dlclark)\
**Post date:** [October 3, 2015, 3:12pm UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995/3 "2015-10-03T15:12:56Z")

</div>

What was your code to create the process group? I’ve had success with that method in the past.

---

<div class="post-metadata">

**Author:** ![VarunKSaini](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/varunksaini/32/251_2.png) [@VarunKSaini](https://forum.golangbridge.org/u/VarunKSaini)\
**Post date:** [October 3, 2015, 3:26pm UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995/4 "2015-10-03T15:26:47Z")

</div>

@dlclark:

```
cmd := exec.Command(execPath, args...)
cmd.Dir = workingDir
cmd.Env = env

//create a new process group
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}

```

and then I am trying:

```
	if err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL); err != nil {
		return 0, fmt.Errorf("Failed to kill process: %v", cmd.Process.Pid, err)
```

---

<div class="post-metadata">

**Author:** ![dlclark](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/dlclark/32/785_2.png) [@dlclark](https://forum.golangbridge.org/u/dlclark)\
**Post date:** [October 3, 2015, 8:33pm UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995/5 "2015-10-03T20:33:04Z")

</div>

Ah, you want to kill the negative of the process group ID to `syscall.Kill()`, not the process ID:

```
pgid, err := syscall.Getpgid(cmd.Process.Pid)
if err == nil {
    if err := syscall.Kill(-pgid, syscall.SIGKILL); err != nil {
        ...
    }
}
```

---

<div class="post-metadata">

**Author:** ![Justin\_Israel](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/justin_israel/32/323_2.png) [@Justin\_Israel](https://forum.golangbridge.org/u/Justin_Israel)\
**Post date:** [October 3, 2015, 9:04pm UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995/6 "2015-10-03T21:04:14Z")

</div>

If the kill is being done from the same parent process that created the  
group, wouldn’t the pid and pgid be the same?

---

<div class="post-metadata">

**Author:** ![VarunKSaini](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/varunksaini/32/251_2.png) [@VarunKSaini](https://forum.golangbridge.org/u/VarunKSaini)\
**Post date:** [October 3, 2015, 9:48pm UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995/7 "2015-10-03T21:48:14Z")

</div>

@dlclark: This one worked, I learned something today 😄

---

<div class="post-metadata">

**Author:** ![dlclark](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/dlclark/32/785_2.png) [@dlclark](https://forum.golangbridge.org/u/dlclark)\
**Post date:** [October 3, 2015, 9:48pm UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995/8 "2015-10-03T21:48:18Z")

</div>

@Justin_Israel is right. I put together a little test program and the group was created (and killable as a set) when I set `cmd.SysProcAttr.Setpgid=false` (the default). Setting that to true creates a unique process group for the pid, which means it’s not killable with its parent.

---

<div class="post-metadata">

**Author:** ![dlclark](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/dlclark/32/785_2.png) [@dlclark](https://forum.golangbridge.org/u/dlclark)\
**Post date:** [October 3, 2015, 9:48pm UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995/9 "2015-10-03T21:48:58Z")

</div>

@VarunKSaini well that’s kind of surprising to me, but glad it worked. 😄

---

<div class="post-metadata">

**Author:** ![Justin\_Israel](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/justin_israel/32/323_2.png) [@Justin\_Israel](https://forum.golangbridge.org/u/Justin_Israel)\
**Post date:** [October 4, 2015, 6:43am UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995/10 "2015-10-04T06:43:24Z")

</div>

@dlclark, I assume it worked because the process was created as a process group leader and then it goes and creates a bunch of child processes which are also inheriting that process group. Then killing the pgid of the process that was started would take down all the children that were in that group. But like you said after having performed a test… killing that main program (the launcher of the first process) wouldn’t take down the child because the child started a new group.

---

<div class="post-metadata">

**Author:** ![VarunKSaini](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/varunksaini/32/251_2.png) [@VarunKSaini](https://forum.golangbridge.org/u/VarunKSaini)\
**Post date:** [October 4, 2015, 11:15am UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995/11 "2015-10-04T11:15:32Z")

</div>

@dlclark @Justin_Israel Well it passed the tests which are basically running a process that creates 1-2 child processes. I still need to test it in production (after code review etc.). But I will like to learn more, isn’t `cmd.SysProcAttr.Setpgid=true` for seeting a group id so that you can kill the whole group instead of orphaning the child processes? Or there is lack of understanding from my side?

---

<div class="post-metadata">

**Author:** ![Tamas\_Gulacsi](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/tamas_gulacsi/32/243_2.png) [@Tamas\_Gulacsi](https://forum.golangbridge.org/u/Tamas_Gulacsi)\
**Post date:** [October 4, 2015, 12:13pm UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995/12 "2015-10-04T12:13:50Z")

</div>

See [https://github.com/tgulacsi/go/tree/master/proc](https://github.com/tgulacsi/go/tree/master/proc)

You should start a new process group for your subprocess, which will start child’s. But you have to clear Setpgid for those childs!

---

<div class="post-metadata">

**Author:** ![VarunKSaini](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/varunksaini/32/251_2.png) [@VarunKSaini](https://forum.golangbridge.org/u/VarunKSaini)\
**Post date:** [October 4, 2015, 1:35pm UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995/13 "2015-10-04T13:35:13Z")

</div>

@Tamas_Gulacsi you suggest using `*os.Process` to kill the process related to command, right? If I am understanding right, my way still has possibilities to spare the child processes?

---

<div class="post-metadata">

**Author:** ![Tamas\_Gulacsi](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/tamas_gulacsi/32/243_2.png) [@Tamas\_Gulacsi](https://forum.golangbridge.org/u/Tamas_Gulacsi)\
**Post date:** [October 4, 2015, 4:05pm UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995/14 "2015-10-04T16:05:54Z")

</div>

As I understood, you spawn a process (son), which will spawn several children (grandchildren); you want to kill the son, and want it to take down its children (the grandchildren), too.

For this, you have to call `os.Exec` with `Cmd.Sysproc.Setpgid = true`, as you found.

What I don’t know, how is the grandchildren spawned? If it is controlled by you, then watch out to not set Setpgid for them, as they need to be in its parent’s group, not in a new.

---

<div class="post-metadata">

**Author:** ![VarunKSaini](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/varunksaini/32/251_2.png) [@VarunKSaini](https://forum.golangbridge.org/u/VarunKSaini)\
**Post date:** [October 4, 2015, 8:46pm UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995/15 "2015-10-04T20:46:05Z")

</div>

Well the children are not spawned by me, this is a build server and it spawns additional processes for tests on the fly, test postgres db etc. So generally it is like one main process and then 5-6 children + grand children processes.

---

<div class="post-metadata">

**Author:** ![Tamas\_Gulacsi](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/tamas_gulacsi/32/243_2.png) [@Tamas\_Gulacsi](https://forum.golangbridge.org/u/Tamas_Gulacsi)\
**Post date:** [October 4, 2015, 9:48pm UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995/16 "2015-10-04T21:48:38Z")

</div>

You can’t kill them if they don’t want you to find them. Systemd’s usage of  
cgroups is for this reason: they can’t escape the cgroup, even with  
demonization (double fork).

So if you want to be sure, use cgroups.  
Seehttps://github.com/kawamuray/cgrun for example.

Varun Saini [forum@golangbridge.org](mailto:forum@golangbridge.org) ezt írta (időpont: 2015. okt. 4., V  
22:51):

---

<div class="post-metadata">

**Author:** ![VarunKSaini](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/varunksaini/32/251_2.png) [@VarunKSaini](https://forum.golangbridge.org/u/VarunKSaini)\
**Post date:** [October 4, 2015, 11:34pm UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995/17 "2015-10-04T23:34:05Z")

</div>

Thanks for the pointer. But I will see if it works with the group id, if not then I have not implement the complex one anyway 😄

---

<div class="post-metadata">

**Author:** ![VarunKSaini](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/varunksaini/32/251_2.png) [@VarunKSaini](https://forum.golangbridge.org/u/VarunKSaini)\
**Post date:** [October 21, 2015, 5:51pm UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995/18 "2015-10-21T17:51:10Z")

</div>

Hey @Tamas_Gulacsi,

Question - So I tried to do same as you are doing in your [https://github.com/tgulacsi/go/proc/procutils.go](https://github.com/tgulacsi/go/proc/procutils.go)

But if I have run a process such as  
`cmd := exec.Command("/bin/sh", "-c", "watch date > date.txt")`

instead of killing the process on timeout, it just returns with success and the process move to PPID = 1. Which is problematic in my case. I want the child processes to kill not the parent one and in my case there is a 5-6 level deep process tree.

Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![Tamas\_Gulacsi](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/tamas_gulacsi/32/243_2.png) [@Tamas\_Gulacsi](https://forum.golangbridge.org/u/Tamas_Gulacsi)\
**Post date:** [October 21, 2015, 7:27pm UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995/19 "2015-10-21T19:27:04Z")

</div>

Can you share some code?

On Linux, the trick is to kill the whole process group, not just a process of it. This is achieved by sending a KILL signal to the negative PID of the process group’s leader.

So if you start a new process with Setpgid=true, then THAT child process will become its own process group’s leader, and also independent of its parent’s process group! So in this case killing tha parent won’t kill this child.

You’re saying this is a 5-6 level deep process tree - if you want to kill all of it, kill the process group’s leader, and ensure that neither child created a separate process group!

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [January 19, 2016, 7:27pm UTC](https://forum.golangbridge.org/t/killing-child-process-on-timeout-in-go-code/995/20 "2016-01-19T19:27:23Z")

</div>

Closed on January 19, 2016, 7:27pm UTC

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