diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/utils/utils.go b/utils/utils.go index 6f6b7fd..bdcc01a 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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 + } + } +} \ No newline at end of file