From a0f54cbe5ac2cd8b8296c2c57c30029dd349cff0 Mon Sep 17 00:00:00 2001 From: Mathias Hall-Andersen Date: Sun, 4 Feb 2018 16:08:26 +0100 Subject: Align with go library layout --- daemon_linux.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 daemon_linux.go (limited to 'daemon_linux.go') diff --git a/daemon_linux.go b/daemon_linux.go new file mode 100644 index 0000000..e1aaede --- /dev/null +++ b/daemon_linux.go @@ -0,0 +1,32 @@ +package main + +import ( + "os" + "os/exec" +) + +/* Daemonizes the process on linux + * + * This is done by spawning and releasing a copy with the --foreground flag + */ +func Daemonize(attr *os.ProcAttr) error { + // I would like to use os.Executable, + // however this means dropping support for Go <1.8 + path, err := exec.LookPath(os.Args[0]) + if err != nil { + return err + } + + argv := []string{os.Args[0], "--foreground"} + argv = append(argv, os.Args[1:]...) + process, err := os.StartProcess( + path, + argv, + attr, + ) + if err != nil { + return err + } + process.Release() + return nil +} -- cgit v1.2.3