The same way you handle any blocking I/O in Go:
package main
import (
"log"
"os/exec"
"time"
)
var nwait = 1 * time.Second // vary this to see the effect
func main() {
log.SetFlags(0)
cmd := exec.Command("sleep", "2")
err := make(chan error)
// run the command and wait for it in a seperate goroutine.
go func() {
err <- cmd.Run()
}()
// sleep for a little bit.
time.Sleep(nwait)
// check the status of the process.
select {
default:
log.Print("process still running")
case e := <-err:
if e != nil {
log.Print("process exited: %s", e)
} else {
log.Print("process exited without error")
}
}
}
Cheers,
Anthony