From a39e9d7b845d3dd4d6ec2a52d29934748c67341e Mon Sep 17 00:00:00 2001 From: caiyuzheng <290198252@qq.com> Date: Mon, 27 Jul 2020 21:56:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AE=88=E6=8A=A4=E8=BF=9B?= =?UTF-8?q?=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + utils/utils.go | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 .gitignore 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