18 Cmd *exec.Cmd `json:"-"`
19 ExitState *os.ProcessState `json:"-"`
20 InputFile io.Reader `json:"-"`
21 OutputFile io.WriteCloser `json:"-"`
22 WaitCh chan struct{} `json:"-"`
25 // execPath: command name
26 // args: args to command. (should not include name)
27 func StartProcess(label string, dir string, execPath string, args []string, inFile io.Reader, outFile io.WriteCloser) (*Process, error) {
28 cmd := exec.Command(execPath, args...)
33 if err := cmd.Start(); err != nil {
41 StartTime: time.Now(),
46 WaitCh: make(chan struct{}),
49 err := proc.Cmd.Wait()
51 // fmt.Printf("Process exit: %v\n", err)
52 if exitError, ok := err.(*exec.ExitError); ok {
53 proc.ExitState = exitError.ProcessState
56 proc.ExitState = proc.Cmd.ProcessState
57 proc.EndTime = time.Now() // TODO make this goroutine-safe
58 err = proc.OutputFile.Close()
60 fmt.Printf("Error closing output file for %v: %v\n", proc.Label, err)
67 func (proc *Process) StopProcess(kill bool) error {
68 defer proc.OutputFile.Close()
70 // fmt.Printf("Killing process %v\n", proc.Cmd.Process)
71 return proc.Cmd.Process.Kill()
73 // fmt.Printf("Stopping process %v\n", proc.Cmd.Process)
74 return proc.Cmd.Process.Signal(os.Interrupt)