Created
June 12, 2012 08:36
-
-
Save madmo/2916217 to your computer and use it in GitHub Desktop.
Simple Daemon
This file contains 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 ( | |
"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