-
Notifications
You must be signed in to change notification settings - Fork 50
/
cmd_runner.go
70 lines (59 loc) · 1.27 KB
/
cmd_runner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//go:build !windows
// +build !windows
package cmd_runner
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"strings"
"syscall"
"time"
)
func CmdRun(scriptPath string, timeout int) error {
// by default timeout should be 3s
if timeout <= 0 {
timeout = 3
}
cmd := exec.Command(strings.TrimRight(scriptPath, "\n"))
cmd.Env = os.Environ()
// Set up a process group which will be killed later
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
}()
select {
case <-time.After(time.Duration(timeout) * time.Second):
// Get process group which we want to kill
pgid, err := syscall.Getpgid(cmd.Process.Pid)
if err != nil {
return err
}
// Send kill to process group
if err := syscall.Kill(-pgid, 15); err != nil {
return err
}
fmt.Printf("Process killed as timeout(%d) reached\n", timeout)
case err := <-done:
if err != nil {
return fmt.Errorf("process finished with error = %v", err)
}
log.Print("Process finished successfully")
}
// Print log
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
m := scanner.Text()
fmt.Println(m)
}
return nil
}