-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshell_unix.go
53 lines (44 loc) · 1023 Bytes
/
shell_unix.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
// +build !windows
package cmd
import (
"io/ioutil"
"log"
"os"
"os/signal"
"runtime/debug"
"strconv"
"syscall"
)
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")
}
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGUSR1, syscall.SIGTERM, syscall.SIGINT)
for sig := range sigChan {
switch sig {
case syscall.SIGUSR1:
cmd.shellCommand(name)
case syscall.SIGTERM, syscall.SIGINT:
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())
}
}()
command, err := ioutil.ReadFile(name + ".cmd")
if err != nil {
log.Printf("read "+name+".cmd failed: %v", err)
return
}
if _, ok := cmd.Process(string(command)); !ok {
log.Printf("unsupported command: %s", command)
}
}