daemon

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2016 License: MIT Imports: 7 Imported by: 0

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

View Source
var ErrStop = errors.New("stop serve signals")

ErrStop should be returned signal handler function for termination of handling signals.

View Source
var ErrWouldBlock = syscall.EWOULDBLOCK

ErrWoldBlock indicates on locking pid-file by another process.

Functions

func Reborn

func Reborn(umask uint32, workDir string) (err error)

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(s, target *os.File) (err error)

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.

func WasReborn

func WasReborn() bool

func WasReborn, return true if the process has environment variable _GO_DAEMON=1 (child process).

Types

type PidFile

type PidFile struct {
	// contains filtered or unexported fields
}

PidFile contains information of pid-file.

func LockPidFile

func LockPidFile(path string, perm os.FileMode) (pidf *PidFile, err error)

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:

func (*PidFile) Unlock

func (pidf *PidFile) Unlock() (err error)

func Unlock unlocks and removes pid-file.

type SignalHandlerFunc

type SignalHandlerFunc func(sig os.Signal) (err error)

SignalHandlerFunc is the interface for signal handler functions.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL