-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd.go
45 lines (39 loc) · 848 Bytes
/
cmd.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
package vproxy
import (
"fmt"
"log"
"os"
"os/exec"
"github.com/shirou/gopsutil/process"
)
func runCommand(args []string) *exec.Cmd {
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
setProcAttr(cmd)
fmt.Println("[*] running command:", cmd)
err := cmd.Start()
if err != nil {
log.Fatal("error starting command: ", err)
}
return cmd
}
func stopCommand(cmd *exec.Cmd) {
if cmd == nil {
return
}
fmt.Println("[*] stopping process", cmd.Process.Pid)
proc, err := process.NewProcess(int32(cmd.Process.Pid))
if err != nil {
if err.Error() == "process does not exist" {
return
}
fmt.Println("[*] warning: error finding child process:", err)
} else {
err = proc.Terminate()
if err != nil {
fmt.Println("[*] error killing child process:", err)
}
}
cmd.Process.Wait()
}