-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshell_win.go
66 lines (55 loc) · 1.23 KB
/
shell_win.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
// +build windows
package cmd
import (
"io/ioutil"
"log"
"os"
"os/signal"
"runtime/debug"
"strconv"
"syscall"
"time"
)
func (cmd *CMD) Shell(name string) {
cmd.initShell(name)
if pid := syscall.Getpid(); pid != 1 {
ioutil.WriteFile(name+".pid", []byte(strconv.Itoa(pid)), 0777)
defer os.Remove(name + ".pid")
}
ticker := time.NewTicker(time.Second * 3)
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)
for {
select {
case <-ticker.C:
cmd.shellCommand(name)
case <-sigChan:
log.Println(name, "killed")
return
}
}
}
func (cmd *CMD) shellCommand(name string) {
defer func() {
if err := recover(); err != nil {
log.Printf("shellCommand() panic: %v\n %s", err, debug.Stack())
}
}()
if _, err := os.Stat(name + ".cmd"); err != nil {
if os.IsNotExist(err) {
return
}
log.Printf("check "+name+".cmd exists failed: %v", err)
}
command, err := ioutil.ReadFile(name + ".cmd")
if err != nil {
log.Printf("read "+name+".cmd failed: %v", err)
return
}
if err := os.Remove(name + ".cmd"); err != nil {
log.Printf("remove "+name+".cmd failed: %v", err)
}
if _, ok := cmd.Process(string(command)); !ok {
log.Printf("unsupported command: %s", command)
}
}