Skip to content

Instantly share code, notes, and snippets.

@akamensky
Created March 9, 2018 11:13
Show Gist options
  • Save akamensky/1a45e4d56c076eb3b6592a1598520da9 to your computer and use it in GitHub Desktop.
Save akamensky/1a45e4d56c076eb3b6592a1598520da9 to your computer and use it in GitHub Desktop.
Easy golang process daemon
package main
import (
"os/exec"
"os"
"time"
)
func init() {
isForked := os.Getenv("DAEMON")
if isForked == "" {
// set ENV
os.Setenv("DAEMON", "1")
// start child process
cmd := exec.Command(os.Args[0], os.Args[1:]...)
cmd.Start()
// can exit now
os.Exit(0)
} else {
// Should close stdin/stdout/stderr here
// as proper daemon should
os.Stdin.Close()
os.Stdout.Close()
os.Stderr.Close()
}
}
func main() {
time.Sleep(60 * time.Second)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment