GolangWebDev
GolangWebDev
596 0 0

Daemonize Your Go Programs

Here is four guidelines to daemonizing programs correctly:

  1. Log to STDOUT
  2. Shut down on SIGTERM/SIGINT
  3. Reload config on SIGHUP
  4. 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


Discussion

Login Topics