2017-07-15 11:41:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
/* Daemonizes the process on linux
|
|
|
|
*
|
|
|
|
* This is done by spawning and releasing a copy with the --foreground flag
|
2017-08-07 13:25:04 +00:00
|
|
|
*
|
|
|
|
* TODO: Use env variable to spawn in background
|
2017-07-15 11:41:02 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|