-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess.go
60 lines (49 loc) · 1.13 KB
/
process.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
package web
import (
"fmt"
"os"
"path/filepath"
"strconv"
)
func pidFileInfo(path string) (pid int, active bool) {
var err error
if _, err = os.Stat(path); err != nil {
return 0, false
}
pid, err = readPidFile(path)
if err != nil {
return 0, false
}
return pid, isProcessRunning(pid)
}
func readPidFile(path string) (int, error) {
data, err := os.ReadFile(filepath.Clean(path))
if err != nil {
return 0, fmt.Errorf("error reading PID file: %w", err)
}
pid, err := strconv.Atoi(string(data))
if err != nil {
return 0, fmt.Errorf("error converting PID from file: %w", err)
}
return pid, nil
}
func killProcess(pid int) error {
process, err := os.FindProcess(pid)
if err != nil {
return fmt.Errorf("error finding process: %w", err)
}
if err = process.Kill(); err != nil {
return fmt.Errorf("error killing process: %w", err)
}
return nil
}
func interruptProcess(pid int) error {
process, err := os.FindProcess(pid)
if err != nil {
return fmt.Errorf("error finding process: %w", err)
}
if err = process.Signal(os.Interrupt); err != nil {
return fmt.Errorf("error interrupting process: %w", err)
}
return nil
}