-
Notifications
You must be signed in to change notification settings - Fork 2
/
pid.go
89 lines (78 loc) · 1.48 KB
/
pid.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
81
82
83
84
85
86
87
88
89
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/signal"
"strconv"
"syscall"
)
var pid_file_name = "cmdsocket.pid"
var pid_log_prefix = "PID : "
func setupPidModule() {
writePidFile()
signalReceive()
}
func writePidFile() {
pid := os.Getpid()
file, err := os.Create(pid_file_name)
if err != nil {
Error.Println("create pid file :", "error : ",err)
return
}
defer file.Close()
pidstr := fmt.Sprint(pid)
_,err = io.WriteString(file,pidstr)
if err != nil {
Error.Println("write config file error : ",err)
return
}
Info.Println("Finished! Pid ",pidstr," successfully generated.")
}
func readPidFromFile() int{
buf,err := ioutil.ReadFile(pid_file_name)
if err != nil {
logPid("read pid file error",err)
return -1
}
pid,err := strconv.Atoi(string(buf))
if err !=nil {
logPid("string convert err :",err)
return -1
}
return pid
}
func signalReceive() {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc,
syscall.SIGHUP)
go func() {
for s := range sigc {
// reload cmd from cmd file
logPid("get signal ",s)
serializeCmds(UserConfig.Cmd)
}
}()
}
func sendSignal() {
pid := readPidFromFile()
if pid <= 0{
logPid("pid num small ")
return
}
proc,err := os.FindProcess(pid)
if err != nil {
logPid("process not found ",err)
return
}
err = proc.Signal(syscall.SIGHUP)
if err != nil {
logPid("send signal err ",err)
}else {
logPid("send signal success")
}
}
func logPid(v ...interface{}) {
Debug.Println(pid_log_prefix,v)
}