This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
/
pidfile.go
executable file
·110 lines (93 loc) · 2.23 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
/*
* Copyright © 2019 Hedzr Yeh.
*/
package dex
import (
"fmt"
"github.com/hedzr/cmdr"
"github.com/hedzr/cmdr-addons/pkg/plugins/dex/sig"
"github.com/hedzr/log/dir"
"gopkg.in/hedzr/errors.v2"
"io/ioutil"
"log"
"os"
"strconv"
)
type pidFileStruct struct {
Path string
}
// var pidfile = &pidFileStruct{}
func newPidFile(filename string) *pidFileStruct {
return &pidFileStruct{
Path: filename,
}
}
func (pf *pidFileStruct) Create(cmd *cmdr.Command) {
// if cmdr.GetBoolR("server.start.in-daemon") {
//
// }
f, err := os.OpenFile(pf.Path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0770)
if err != nil {
panic(errors.New("Failed to create pid file %q", pf.Path).Attach(err))
}
defer f.Close()
f.WriteString(fmt.Sprintf("%v", os.Getpid()))
}
func (pf *pidFileStruct) Destroy() {
// if cmdr.GetBoolR("server.start.in-daemon") {
// //
// }
if dir.FileExists(pf.Path) {
err := os.RemoveAll(pf.Path)
if err != nil {
panic(errors.New("Failed to destroy pid file %q", pf.Path).Attach(err))
}
}
}
type loggerStruct struct {
}
var logger = &loggerStruct{}
func (l *loggerStruct) Setup(cmd *cmdr.Command) {
//
}
func pidExistsDeep(pid int) (bool, error) {
// pid, err := strconv.ParseInt(p, 0, 64)
// if err != nil {
// log.Fatal(err)
// }
process, err := os.FindProcess(int(pid))
if err != nil {
// fmt.Printf("Failed to find process: %s\n", err)
return false, nil
}
err = sig.SendNilSig(process)
log.Printf("process.Signal on pid %d returned: %v\n", pid, err)
return err == nil, err
}
// IsPidFileExists checks if the pid file exists or not
func IsPidFileExists() bool {
// check if daemon already running.
if _, err := os.Stat(pd.PidFileName()); err == nil {
return true
}
return false
}
// FindDaemonProcess locates the daemon process if running
func FindDaemonProcess() (present bool, process *os.Process) {
if IsPidFileExists() {
s, _ := ioutil.ReadFile(pd.PidFileName())
pid, err := strconv.ParseInt(string(s), 0, 64)
if err != nil {
log.Fatal(err)
}
log.Printf("cat %v ... pid = %v", pd.PidFileName(), pid)
process, err = os.FindProcess(int(pid))
if err == nil {
present = true
}
} else {
log.Printf("cat %v ... app stopped", pd.PidFileName())
}
return
}
const nullDev = "/dev/null"