diff --git a/basher.go b/basher.go index 76bd3c9..b30e12e 100644 --- a/basher.go +++ b/basher.go @@ -22,7 +22,12 @@ func exitStatus(err error) (int, error) { // There is no platform independent way to retrieve // the exit code, but the following will work on Unix if status, ok := exiterr.Sys().(syscall.WaitStatus); ok { - return int(status.ExitStatus()), nil + if status.ExitStatus() != -1 { + return status.ExitStatus(), err + } else { + // The process hasn't exited or was terminated by a signal. + return int(status), err + } } } return 0, err @@ -260,17 +265,23 @@ func (c *Context) Run(command string, args []string) (int, error) { cmd.Stdin = c.Stdin cmd.Stdout = c.Stdout cmd.Stderr = c.Stderr - if err2 := cmd.Start(); err2 != nil { - return 0, err2 + if err := cmd.Start(); err != nil { + return 0, err } - + errChan := make(chan error, 1) go func() { for sig := range signals { - cmd.Process.Signal(sig) - if cmd.ProcessState != nil && !cmd.ProcessState.Exited() { - cmd.Process.Signal(sig) + if sig != syscall.SIGCHLD { + err = cmd.Process.Signal(sig) + if err != nil { + errChan <- err + } } } }() - return exitStatus(cmd.Wait()) + go func() { + errChan <- cmd.Wait() + }() + err = <-errChan + return exitStatus(err) }