Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Daemon
package main
import (
"bitbucket.org/madmo/daemonize"
"fmt"
"log"
"log/syslog"
"os"
"os/signal"
)
func main() {
// Syslog logging
logger, err := syslog.New(syslog.LOG_EMERG, "go-daemon")
if err != nil {
log.Printf("Can't open syslog connection: %v", err)
return
}
// Become a daemon
p, err := daemonize.Daemonize(false, false)
if err != nil {
// Daemonize failed
logger.Emerg(fmt.Sprintf("Can't daemonize: %v", err))
return
}
// We are the parent, exit
if p != nil {
return
}
// Do Daemon stuff here
logger.Info("Daemon up and running")
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, os.Kill)
logger.Info(fmt.Sprintf("Got signal (%s), exiting", (<-sigs).String()))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment