-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
80 lines (68 loc) · 1.59 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
package main
import (
"log"
"os"
"syscall"
"time"
"github.com/mitchellh/go-ps"
)
func main() {
//cmd := exec.Command("sleep", "10")
//cmdOutput := &bytes.Buffer{}
//cmd.Stdout = cmdOutput
//
//err := cmd.Start()
//if err != nil {
// // Run could also return this error and push the program
// // termination decision to the `main` method.
// log.Fatal(err)
//}
//
//result := make(chan error, 1)
//go func() {
// result <- cmd.Wait()
//}()
//cmd, err := exec.LookPath("sleep")
//if err != nil {
// log.Fatal(err)
//}
// You need to run the program as root to do this
var cred = &syscall.Credential{Uid: 502, Gid: 20, Groups: []uint32{}, NoSetGroups: false}
// the Noctty flag is used to detach the process from parent tty
var sysproc = &syscall.SysProcAttr{Credential: cred, GidMappingsEnableSetgroups: true}
var attr = os.ProcAttr{
Dir: ".",
Env: os.Environ(),
Sys: sysproc,
}
result := make(chan bool, 1)
go func() {
process, err := os.StartProcess("/bin/sleep", []string{"/bin/sleep", "15"}, &attr)
if err == nil {
state, err := process.Wait()
if err != nil {
panic(err)
}
result <- state.Success()
} else {
panic(err)
}
}()
list, err := ps.Processes()
if err != nil {
panic(err)
}
for _, p := range list {
log.Printf("Process %s with PID %d and PPID %d", p.Executable(), p.Pid(), p.PPid())
}
select {
case r := <-result:
if !r {
log.Fatal("could not execute sleep command,error:", err)
}
log.Println("everything worked, closing channel")
close(result)
case <-time.After(20 * time.Second):
log.Println("timeout")
}
}