Documentation ¶
Overview ¶
Package daemon provides function to daemonization processes. And such as the handling of system signals and the pid-file creation.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrStop = errors.New("stop serve signals")
ErrStop should be returned signal handler function for termination of handling signals.
var ErrWouldBlock = syscall.EWOULDBLOCK
ErrWoldBlock indicates on locking pid-file by another process.
Functions ¶
func Reborn ¶
func Reborn daemonize process. Function Reborn calls ForkExec in the parent process and terminates him. In the child process, function sets umask, work dir and calls Setsid. Function sets for child process environment variable _GO_DAEMON=1 - the mark, might used for debug.
Example ¶
err := daemon.Reborn(027, "/") if err != nil { log.Println("Error:", err) os.Exit(1) } daemon.ServeSignals()
Output:
func RedirectStream ¶
func RedirectStream redirects file s to file target.
Example ¶
file, err := os.OpenFile("/tmp/daemon-log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600) if err != nil { os.Exit(1) } if err = daemon.RedirectStream(os.Stdout, file); err != nil { os.Exit(2) } if err = daemon.RedirectStream(os.Stderr, file); err != nil { os.Exit(2) } file.Close() fmt.Println("some message") log.Println("some message")
Output:
func ServeSignals ¶
func ServeSignals() (err error)
func ServeSignals calls handlers for system signals.
Example ¶
TermHandler := func(sig os.Signal) error { log.Println("SIGTERM:", sig) return daemon.ErrStop } HupHandler := func(sig os.Signal) error { log.Println("SIGHUP:", sig) return nil } daemon.SetHandler(TermHandler, syscall.SIGTERM, syscall.SIGKILL) daemon.SetHandler(HupHandler, syscall.SIGHUP) err := daemon.ServeSignals() if err != nil { log.Println("Error:", err) }
Output:
func SetHandler ¶
func SetHandler(handler SignalHandlerFunc, signals ...os.Signal)
func SetHandler sets handler for the given signals. SIGTERM has the default handler, he returns ErrStop.
Types ¶
type PidFile ¶
type PidFile struct {
// contains filtered or unexported fields
}
PidFile contains information of pid-file.
func LockPidFile ¶
func LockPidFile trys create and lock pid-file.
Example ¶
pidf, err := daemon.LockPidFile("name.pid", 0600) if err != nil { if err == daemon.ErrWouldBlock { log.Println("daemon already exists") } else { log.Println("pid file creation error:", err) } return } defer pidf.Unlock() daemon.ServeSignals()
Output:
type SignalHandlerFunc ¶
SignalHandlerFunc is the interface for signal handler functions.