2017-07-15 13:41:02 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2017-11-17 17:25:45 +01:00
|
|
|
"os/exec"
|
2017-07-15 13:41:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
/* Daemonizes the process on linux
|
|
|
|
*
|
|
|
|
* This is done by spawning and releasing a copy with the --foreground flag
|
|
|
|
*/
|
2017-11-14 18:26:28 +01:00
|
|
|
func Daemonize(attr *os.ProcAttr) error {
|
2017-11-17 17:25:45 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2017-07-15 13:41:02 +02:00
|
|
|
argv := []string{os.Args[0], "--foreground"}
|
|
|
|
argv = append(argv, os.Args[1:]...)
|
|
|
|
process, err := os.StartProcess(
|
2017-11-17 17:25:45 +01:00
|
|
|
path,
|
2017-07-15 13:41:02 +02:00
|
|
|
argv,
|
|
|
|
attr,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
process.Release()
|
|
|
|
return nil
|
|
|
|
}
|