title: Check if exec.Cmd is done running in Go
body: <p>I'm trying to check if an <code>exec.Cmd</code> <strong>is running</strong> in these scenarios:</p>
  
  <ol>
  <li>Before I actually started the command</li>
  <li>After the command has started, but before it finished</li>
  <li>After the command has finished</li>
  </ol>
  
  <p>This will allow me to kill this command if it is running so that I can start it again with different parameters.</p>
  
  <p>A simple use case below:</p>
  
  <pre><code>c := exec.Command("omxplayer", "video.mp4")
  s, _ := c.StdinPipe() // use the pipe to send the "q" string to quit omxplayer
  
  log.Printf("Running (false): %v\n", checkRunning(c)) // prints false: command has not started yet
  c.Start()
  log.Printf("Running (true): %v\n", checkRunning(c)) // print true: command has started
  
  time.AfterFunc(3*time.Second, func() {
    log.Println("about to quit process...")
    log.Printf("Running (true): %v\n", checkRunning(c)) // prints true: command still running at this point
    s.Write([]byte("q"))
  })
  
  log.Println("waiting for command to end")
  log.Printf("Running (true): %v\n", checkRunning(c)) // prints true: command still running at this point
  c.Wait()
  log.Println("command should have ended by now")
  log.Printf("Running (false): %v\n", checkRunning(c)) // prints false: command ended at this point
  </code></pre>
  
  <p>Here's the best that I could come up with:</p>
  
  <pre><code>func checkRunning(cmd *exec.Cmd) bool {
    if cmd == nil || cmd.ProcessState != nil &amp;&amp; cmd.ProcessState.Exited() || cmd.Process == nil {
      return false
    }
  
    return true
  }
  </code></pre>
  
  <p>It works for the use case above, but it seems overly complicated and I'm not sure how reliable it is.</p>
  
  <p>Is there a better way?</p>