Created
March 9, 2018 11:13
-
-
Save akamensky/1a45e4d56c076eb3b6592a1598520da9 to your computer and use it in GitHub Desktop.
Easy golang process daemon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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