Daemonize Your Go Programs
Here is four guidelines to daemonizing programs correctly:
- Log to
STDOUT
- Shut down on
SIGTERM/SIGINT
- Reload config on
SIGHUP
- Provide the necessary config file for your favorite init system to control your daemon
go func() {
select {
case s := <-signalChan:
switch s {
case syscall.SIGINT, syscall.SIGTERM:
log.Printf("Got SIGINT/SIGTERM, exiting.")
cancel()
os.Exit(1)
case syscall.SIGHUP:
log.Printf("Got SIGHUP, reloading.")
c.init(os.Args)
}
case <-ctx.Done():
log.Printf("Done.")
os.Exit(1)
}
}()
0
See Also
- Optimizing Large File Transfers in Linux with Go TCP/Syscall
- Faster queues in Go
- Get current Func and Interface name in Go
- Nano ID implementation in Go -- unique ID generator
- Generate and composite thumbnails from images using Go