添加守护进程

master
caiyuzheng 2020-07-27 21:56:33 +08:00
parent f06e194bf1
commit a39e9d7b84
2 changed files with 20 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.idea/

View File

@ -3,7 +3,10 @@ package utils
import ( import (
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"os"
"path/filepath"
"reflect" "reflect"
"runtime"
"strings" "strings"
) )
@ -38,3 +41,19 @@ func InArrary(arr interface{}, ele interface{}) int {
} }
return 1 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
}
}
}