siddontang.golib/exec2/exec.go

37 lines
543 B
Go
Raw Normal View History

2014-09-13 17:00:14 +08:00
package exec2
2014-04-22 15:03:07 +08:00
import (
"os/exec"
"time"
)
//exec another process
//if wait d Duration, it will kill the process
//d is <= 0, wait forever
func ExecTimeout(d time.Duration, name string, args ...string) error {
cmd := exec.Command(name, args...)
if err := cmd.Start(); err != nil {
return err
}
if d <= 0 {
return cmd.Wait()
}
done := make(chan error)
go func() {
done <- cmd.Wait()
}()
select {
case <-time.After(d):
cmd.Process.Kill()
//wait goroutine return
return <-done
case err := <-done:
return err
}
}