-
Notifications
You must be signed in to change notification settings - Fork 1
/
pid.go
62 lines (51 loc) · 1.16 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
// Copyright 2019 The vogo Authors. All rights reserved.
// author: wongoo
package gracego
import (
"fmt"
"os"
"strings"
)
var (
enableWritePid = false
pidFileDir string
pidFilePath string
)
// EnableWritePid enable to write pid file
// dir - the directory where to write pid file
func EnableWritePid(dir string) error {
if dir == "" {
dir = os.TempDir()
} else if _, err := os.Stat(dir); err != nil {
return err
}
if !strings.HasSuffix(dir, string(os.PathSeparator)) {
dir += string(os.PathSeparator)
}
pidFileDir = dir
enableWritePid = true
return nil
}
func writePidFile() {
if !enableWritePid {
return
}
if pidFilePath == "" {
pidFilePath = fmt.Sprintf("%s%s.pid", pidFileDir, serverName)
graceLog("set pid file: %s", pidFilePath)
}
pidFile, err := os.OpenFile(pidFilePath, os.O_RDWR, 0660)
if err != nil {
pidFile, err = os.Create(pidFilePath)
if err != nil {
graceLog("failed to create pid file %s, error: %v", pidFilePath, err)
return
}
}
defer pidFile.Close()
pid := fmt.Sprint(os.Getpid())
_, err = pidFile.WriteString(pid)
if err != nil {
graceLog("failed to write pid file %s, error: %v", pidFilePath, err)
}
}