On Tue, May 16, 2017 at 4:56 AM,  <yuri.shakhma...@gmail.com> wrote:
>
> I have some code:
>
> ...
>
> func RunTimeout(c *exec.Cmd, cnl context.CancelFunc) error {
>        defer cnl()
>        if err := c.Start(); err != nil {
>               return err
>        }
>
>        return c.Wait()
> }
>
> func main() {
>        ctx, cnl := context.WithTimeout(context.Background(), 10*time.Second)
>
>        if err := RunTimeout(exec.CommandContext(ctx, "test.sh"), cnl); err
> != nil {
>               if err == context.DeadlineExceeded {
>                      println("timed out")
>               } else {
>                      println(err.Error())
>               }
>        }
> }
>
> Script test.sh looks like:
> #!/bin/bash
> sleep 60s
>
>
> If exec.CommandContext timed out it would kill process. But in my case there
> are two process: parent bash process test.sh and its child process with
> sleep 60s. In this case only bash process test.sh will be killed after 10
> seconds.
>
> Is this normal behavior? It seems strange.

Yes, that is expected behavior.  Your use of /bin/bash suggests that
you are using a Unix system (you didn't say), and on a Unix system
there is no general mechanism for sending a signal to a process and
all of its children.  You can use SysProcAttr.Setpgid to put the child
process into a new process group, and then use syscall.Kill with a
negative number to send a signal to all members of that process group,
but that has various other consequences and exec.CommandContext won't
do it for you.

Ian

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to