summaryrefslogtreecommitdiffhomepage
path: root/src/daemon_linux.go
blob: 809c176de313d6f433b9dd5399b6859894ece9dc (plain)
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
33
34
package main

import (
	"os"
)

/* Daemonizes the process on linux
 *
 * This is done by spawning and releasing a copy with the --foreground flag
 */

func Daemonize() error {
	argv := []string{os.Args[0], "--foreground"}
	argv = append(argv, os.Args[1:]...)
	attr := &os.ProcAttr{
		Dir: ".",
		Env: os.Environ(),
		Files: []*os.File{
			os.Stdin,
			nil,
			nil,
		},
	}
	process, err := os.StartProcess(
		argv[0],
		argv,
		attr,
	)
	if err != nil {
		return err
	}
	process.Release()
	return nil
}