添加守护进程
parent
f06e194bf1
commit
a39e9d7b84
|
@ -0,0 +1 @@
|
|||
.idea/
|
|
@ -3,7 +3,10 @@ package utils
|
|||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
@ -38,3 +41,19 @@ func InArrary(arr interface{}, ele interface{}) int {
|
|||
}
|
||||
return 1
|
||||
}
|
||||
// 将程序变成守护进程。
|
||||
func Daemonize() {
|
||||
if runtime.GOOS == "windows"{
|
||||
// windows不支持进程守护,直接return
|
||||
return
|
||||
}
|
||||
if runtime.GOOS == "linux"{
|
||||
if os.Getppid()!= 1{ //判断当其是否是子进程,当父进程return之后,子进程会被 系统1 号进程接管
|
||||
filePath,_:=filepath.Abs(os.Args[0]) //将命令行参数中执行文件路径转换成可用路径
|
||||
os.StartProcess(filePath,os.Args[1:],&os.ProcAttr{Files:[]*os.File{os.Stdin,os.Stdout,os.Stderr}})
|
||||
os.Exit(0)
|
||||
}else{
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue