/
pidfile.go
executable file
·140 lines (122 loc) · 3.17 KB
/
pidfile.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright © 2019 Hedzr Yeh.
*/
package impl
import (
"fmt"
"github.com/hedzr/cmdr"
"io/ioutil"
"log"
"os"
"strconv"
)
// Stop stops the daemon process if running
func Stop(appName string, ctx *Context) {
present, process := FindDaemonProcess(ctx)
if present {
const prefix = "server.stop"
var err error
switch {
case cmdr.GetBoolRP(prefix, "hup"):
log.Printf("sending SIGHUP to pid %v", process.Pid)
err = sigSendHUP(process)
case cmdr.GetBoolRP(prefix, "quit"):
log.Printf("sending SIGQUIT to pid %v", process.Pid)
err = sigSendQUIT(process)
case cmdr.GetBoolRP(prefix, "kill"):
log.Printf("sending SIGKILL to pid %v", process.Pid)
err = sigSendKILL(process)
case cmdr.GetBoolRP(prefix, "usr2"):
log.Printf("sending SIGUSR2 to pid %v", process.Pid)
err = sigSendUSR2(process)
case cmdr.GetBoolRP(prefix, "term"):
fallthrough
default:
log.Printf("sending SIGTERM to pid %v", process.Pid)
err = sigSendTERM(process)
}
if err != nil {
log.Fatal(err)
return
}
} else {
fmt.Printf("%v is stopped.\n", appName)
}
}
// Reload reloads the daemon process if running
func Reload(appName string, ctx *Context) {
present, process := FindDaemonProcess(ctx)
if present {
log.Printf("sending SIGHUP to pid %v", process.Pid)
if err := sigSendHUP(process); err != nil {
log.Fatal(err)
return
}
} else {
fmt.Printf("%v is stopped.\n", appName)
}
}
// HotReload reloads the daemon process if running
func HotReload(appName string, ctx *Context) {
present, process := FindDaemonProcess(ctx)
if present {
log.Printf("sending SIGUSR2 to pid %v", process.Pid)
if err := sigSendUSR2(process); err != nil {
log.Fatal(err)
return
}
Stop(appName, ctx)
} else {
fmt.Printf("%v is stopped.\n", appName)
}
}
// FindDaemonProcess locates the daemon process if running
func FindDaemonProcess(ctx *Context) (present bool, process *os.Process) {
if IsPidFileExists(ctx) {
s, _ := ioutil.ReadFile(ctx.PidFileName)
pid, err := strconv.ParseInt(string(s), 0, 64)
if err != nil {
log.Fatal(err)
}
log.Printf("cat %v ... pid = %v", ctx.PidFileName, pid)
process, err = os.FindProcess(int(pid))
if err == nil {
present = true
}
} else {
log.Printf("cat %v ... app stopped", ctx.PidFileName)
}
return
}
// IsPidFileExists checks if the pid file exists or not
func IsPidFileExists(ctx *Context) bool {
// check if daemon already running.
if _, err := os.Stat(ctx.PidFileName); err == nil {
return true
}
return false
}
func removePID(ctx *Context) {
// remove PID file
_ = os.Remove(ctx.PidFileName)
}
func savePID(pid int, ctx *Context) {
file, err := os.Create(ctx.PidFileName)
if err != nil {
log.Printf("Unable to create pid file : %v\n", err)
os.Exit(ErrnoForkAndDaemonFailed)
}
defer file.Close()
_, err = file.WriteString(strconv.Itoa(pid))
log.Printf("pid %v written into %v", pid, ctx.PidFileName)
if err != nil {
log.Printf("Unable to create pid file : %v\n", err)
os.Exit(ErrnoForkAndDaemonFailed)
}
err = file.Sync() // flush to disk
if err != nil {
log.Printf("Unable to flush pid file : %v\n", err)
os.Exit(ErrnoForkAndDaemonFailed)
}
}
// var PIDFile = "/tmp/daemonize.pid"