|
@ -29,6 +29,7 @@ type Process struct { |
|
|
Cmd *exec.Cmd `json:"-"` |
|
|
Cmd *exec.Cmd `json:"-"` |
|
|
ExitState *os.ProcessState `json:"-"` |
|
|
ExitState *os.ProcessState `json:"-"` |
|
|
OutputFile *os.File `json:"-"` |
|
|
OutputFile *os.File `json:"-"` |
|
|
|
|
|
WaitCh chan struct{} `json:"-"` |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const ( |
|
|
const ( |
|
@ -56,8 +57,6 @@ func Create(mode int, label string, execPath string, args []string, input string |
|
|
} |
|
|
} |
|
|
if err := cmd.Start(); err != nil { |
|
|
if err := cmd.Start(); err != nil { |
|
|
return nil, err |
|
|
return nil, err |
|
|
} else { |
|
|
|
|
|
fmt.Printf("Success!") |
|
|
|
|
|
} |
|
|
} |
|
|
proc := &Process{ |
|
|
proc := &Process{ |
|
|
Label: label, |
|
|
Label: label, |
|
@ -68,23 +67,22 @@ func Create(mode int, label string, execPath string, args []string, input string |
|
|
Cmd: cmd, |
|
|
Cmd: cmd, |
|
|
ExitState: nil, |
|
|
ExitState: nil, |
|
|
OutputFile: outFile, |
|
|
OutputFile: outFile, |
|
|
|
|
|
WaitCh: make(chan struct{}), |
|
|
} |
|
|
} |
|
|
go func() { |
|
|
go func() { |
|
|
Wait(proc) |
|
|
|
|
|
|
|
|
err := proc.Cmd.Wait() |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
fmt.Printf("Process exit: %v\n", err) |
|
|
|
|
|
if exitError, ok := err.(*exec.ExitError); ok { |
|
|
|
|
|
proc.ExitState = exitError.ProcessState |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
proc.EndTime = time.Now() // TODO make this goroutine-safe
|
|
|
proc.EndTime = time.Now() // TODO make this goroutine-safe
|
|
|
|
|
|
close(proc.WaitCh) |
|
|
}() |
|
|
}() |
|
|
return proc, nil |
|
|
return proc, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func Wait(proc *Process) error { |
|
|
|
|
|
exitErr := proc.Cmd.Wait() |
|
|
|
|
|
if exitErr != nil { |
|
|
|
|
|
fmt.Printf("Process exit: %v\n", exitErr) |
|
|
|
|
|
proc.ExitState = exitErr.(*exec.ExitError).ProcessState |
|
|
|
|
|
} |
|
|
|
|
|
return exitErr |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func Stop(proc *Process, kill bool) error { |
|
|
func Stop(proc *Process, kill bool) error { |
|
|
if kill { |
|
|
if kill { |
|
|
return proc.Cmd.Process.Kill() |
|
|
return proc.Cmd.Process.Kill() |
|
|