-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpid.go
41 lines (37 loc) · 774 Bytes
/
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
package main
import (
"fmt"
"os"
"path/filepath"
)
// PID is a process id struct
type PID struct {
ID int
FileName string
}
// Write PID struct (you can set filename p.FileName = "/var/run/pid")
// it uses "/run/executable_name/pid" by default
func (p *PID) Write() error {
p.ID = os.Getpid()
if len(p.FileName) < 2 {
binary := filepath.Base(os.Args[0])
p.FileName = fmt.Sprintf("/run/%s/pid", binary)
}
f, err := os.Create(p.FileName)
defer f.Close()
if err != nil {
return err
}
fmt.Fprintf(f, "%d\n", p.ID)
return err
}
// Read PID struct (p.FileName has to be set by the caller)
func (p *PID) Read() error {
f, err := os.Open(p.FileName)
defer f.Close()
if err != nil {
return err
}
_, err = fmt.Fscanf(f, "%d", &p.ID)
return err
}