-
Notifications
You must be signed in to change notification settings - Fork 1
/
runtime.go
74 lines (68 loc) · 1.26 KB
/
runtime.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
package kitgo
import (
"os"
"os/exec"
"os/signal"
"path/filepath"
"syscall"
)
//-------------------------------------
//
//
//
//-------------------------------------
func WaitExitSignal() os.Signal {
signalChan := make(chan os.Signal)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
return <-signalChan
}
//-------------------------------------
//
//
//
//-------------------------------------
func RuntimePath() string {
file, _ := exec.LookPath(os.Args[0])
path, _ := filepath.Abs(file)
return path
}
//-------------------------------------
//
//
//
//-------------------------------------
func ExceptionCatch() interface{} {
err := recover()
if err != nil {
ErrorLog.Printf("尝试恢复, 出错原因 => %v", err)
}
return err
}
//-------------------------------------
//
// 使用守护进程启动
//
//-------------------------------------
func RunDaemon() error {
if os.Getppid() != 1 {
return Restart()
}
return nil
}
//-------------------------------------
//
// 重启
//
//-------------------------------------
func Restart() error {
fp, err := filepath.Abs(os.Args[0])
if err != nil {
return err
}
cmd := exec.Command(fp, os.Args[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Start()
return nil
}