H
H
hitakiri2017-08-01 12:07:43
go
hitakiri, 2017-08-01 12:07:43

How can you track the termination of a running process in Go?

There is a slice of lines from the path to files of different resolutions (.doc, pdf, .dwg, .step, etc.)
They must be run in the order in which they are written to the slice, only after the previous process is completed. Actually, the question arises - how to correctly track the completion of processes of various programs and are there more functional ways to track the work of a third-party process?
I'm currently monitoring with cmd.Wait :

func start(r []string) {
  start := false

  for i := range r {
    cmd := exec.Command("cmd", "/C "+r[i])
    cmd.Start()
    err := cmd.Wait()
    for !start {

      var waitStatus syscall.WaitStatus

        if err != nil {
          os.Stderr.WriteString(fmt.Sprintf("Error: %s\n", err.Error()))
        }
        if exitError, ok := err.(*exec.ExitError); ok {
          waitStatus = exitError.Sys().(syscall.WaitStatus)
          fmt.Printf("Output: %s\n", []byte(fmt.Sprintf("%d", waitStatus.ExitStatus())))
        }
      } else {
        waitStatus = cmd.ProcessState.Sys().(syscall.WaitStatus)
        fmt.Printf("Output: %s\n", []byte(fmt.Sprintf("%d", waitStatus.ExitStatus())))
        start = true
      }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2017-08-01
@hitakiri

cmd.Wait() is quite the right way.
If you need to see the response of the command, then the easiest way is to use exec.Command(.....).CombinedOutput()
It will return the response and wait for the command to complete.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question