aboutsummaryrefslogtreecommitdiffstats
path: root/daemon_linux.go
blob: e1aaede3806624ea1a5c84cd3bdf4fa13d38734f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
}