Documentation
¶
Overview ¶
Package godaemon help program to be deamonized.
Example ¶
signal := flag.String("s", "", "send signal to daemon") handler := func(sig os.Signal) error { log.Println("signal:", sig) if sig == syscall.SIGTERM { return ErrStop } return nil } // Define command: command-line arg, system signal and handler AddCommand(StringFlag(signal, "term"), syscall.SIGTERM, handler) AddCommand(StringFlag(signal, "reload"), syscall.SIGHUP, handler) flag.Parse() // Define daemon context dmn := &Context{ PidFileName: "/var/run/daemon.pid", PidFilePerm: 0o644, LogFileName: "/var/log/daemon.log", LogFilePerm: 0o640, WorkDir: "/", Umask: 0o27, } // Send commands if needed if len(ActiveFlags()) > 0 { d, err := dmn.Search() if err != nil { log.Fatalln("Unable send signal to the daemon:", err) } SendCommands(d) return } // Process daemon operations - send signal if present flag or daemonize child, err := dmn.Reborn() if err != nil { log.Fatalln(err) } if child != nil { return } defer dmn.Release() // Run main operation go func() { for { time.Sleep(0) } }() err = ServeSignals() if err != nil { log.Println("Error:", err) }
Output:
Index ¶
- Constants
- Variables
- func AddCommand(f Flag, sig os.Signal, handler SignalHandlerFunc)
- func AddFlag(f Flag, sig os.Signal)
- func ClearReborn() error
- func Daemonize(optionFns ...OptionFn)
- func Flags() map[Flag]os.Signal
- func ReadPidFile(name string) (pid int, err error)
- func SendCommands(p *os.Process) (err error)
- func ServeSignals() (err error)
- func SetSigHandler(handler SignalHandlerFunc, signals ...os.Signal)
- func WasReborn() bool
- type Context
- type Credential
- type Flag
- type LockFile
- type Option
- type OptionFn
- type SignalHandlerFunc
Examples ¶
Constants ¶
const ( MarkName = "_GO_DAEMON" MarkValue = "1" )
MarkName marks of daemon process - system environment variable _GO_DAEMON={pid}
const FilePerm = os.FileMode(0o640)
FilePerm is the default file permissions for log and pid files.
Variables ¶
var ErrStop = errors.New("stop serve signals")
ErrStop should be returned signal handler function for termination of handling signals.
var ErrWouldBlock = errors.New("daemon: Resource temporarily unavailable")
ErrWouldBlock indicates on locking pid-file by another process.
Functions ¶
func AddCommand ¶
func AddCommand(f Flag, sig os.Signal, handler SignalHandlerFunc)
AddCommand is wrapper on AddFlag and SetSigHandler functions.
func ReadPidFile ¶
ReadPidFile reads process id from file with give name and returns pid. If unable read from a file, returns error.
func SendCommands ¶
SendCommands sends active signals to the given process.
func SetSigHandler ¶
func SetSigHandler(handler SignalHandlerFunc, signals ...os.Signal)
SetSigHandler sets handler for the given signals. SIGTERM has the default handler, he returns ErrStop.
Types ¶
type Context ¶
type Context struct { // Credential holds user and group identities to be assumed by a daemon-process. Credential *Credential // If WorkDir is non-empty, the child changes into the directory before // creating the process. WorkDir string // If PidFileName is non-empty, parent process will try to create and lock // pid file with given name. Child process writes process id to file. PidFileName string // If Chroot is non-empty, the child changes root directory Chroot string // If LogFileName is non-empty, parent process will create file with given name // and will link to fd 2 (stderr) for child process. LogFileName string // If Env is non-nil, it gives the environment variables for the // daemon-process in the form returned by os.Environ. // If it is nil, the result of os.Environ will be used. Env []string // If Args is non-nil, it gives the command-line args for the // daemon-process. If it is nil, the result of os.Args will be used. Args []string // If Umask is non-zero, the daemon-process call Umask() func with given value. Umask int // Permissions for new pid file. PidFilePerm os.FileMode // Permissions for new log file. LogFilePerm os.FileMode // contains filtered or unexported fields }
A Context describes daemon context.
func (*Context) Reborn ¶
Reborn runs second copy of current process in the given context. function executes separate parts of code in child process and parent process and provides demonization of child process. It looks similar as the fork-daemonization, but goroutine-safe. In success returns *os.Process in parent process and nil in child process. Otherwise, returns error.
type Credential ¶
type Flag ¶
type Flag interface {
IsSet() bool
}
Flag is the interface implemented by an object that has two state: 'set' and 'unset'.
func ActiveFlags ¶
func ActiveFlags() (ret []Flag)
ActiveFlags returns flags that has the state 'set'.
func BoolFlag ¶
BoolFlag returns new object that implements interface Flag and has state 'set' when var with the given address is true.
func StringFlag ¶
StringFlag returns new object that implements interface Flag and has state 'set' when var with the given address equals given value of v.
type LockFile ¶
LockFile wraps *os.File and provide functions for locking of files.
func CreatePidFile ¶
CreatePidFile opens the named file, applies exclusive lock and writes current process id to file.
func NewLockFile ¶
NewLockFile returns a new LockFile with the given File.
func OpenLockFile ¶
OpenLockFile opens the named file with flags os.O_RDWR|os.O_CREATE and specified perm. If successful, function returns LockFile for opened file.
func (*LockFile) Lock ¶
Lock apply exclusive lock on an open file. If file already locked, returns error.
func (*LockFile) ReadPid ¶
ReadPid reads process id from file and returns pid. If unable read from a file, returns error.
type Option ¶
type Option struct { Daemon bool Debug bool LogFileName string ParentProcess func(child *os.Process) }
Option is options for Daemonize function.
type OptionFn ¶
type OptionFn func(option *Option)
OptionFn is an option function prototype.
func WithLogFileName ¶
WithLogFileName set the log file name.
func WithParentProcess ¶
WithParentProcess set the custom parent processor, if not set, the default is os.Exit(0)
type SignalHandlerFunc ¶
SignalHandlerFunc is the interface for signal handler functions.