Categorygithub.com/sevlyar/go-daemon
modulepackage
0.1.6
Repository: https://github.com/sevlyar/go-daemon.git
Documentation: pkg.go.dev

# README

go-daemon Build Status GoDoc

Library for writing system daemons in Go.

Now supported only UNIX-based OS (Windows is not supported). But the library was tested only on Linux and OSX, so that if you have an ability to test the library on other platforms, give me feedback, please (#26).

Please, feel free to send me bug reports and fixes. Many thanks to all contributors.

Features

  • Goroutine-safe daemonization;
  • Out of box work with pid-files;
  • Easy handling of system signals;
  • The control of a daemon.

Installation

go get github.com/sevlyar/go-daemon

You can use gopkg.in:

go get gopkg.in/sevlyar/go-daemon.v0

If you want to use the library in production project, please use vendoring, because i can not ensure backward compatibility before release v1.0.

Examples

Documentation

godoc.org/github.com/sevlyar/go-daemon

How it works

We can not use fork syscall in Golang's runtime, because child process doesn't inherit threads and goroutines in that case. The library uses a simple trick: it runs its own copy with a mark - a predefined environment variable. Availability of the variable for the process means an execution in the child's copy. So that if the mark is not setted - the library executes parent's operations and runs its own copy with mark, and if the mark is setted - the library executes child's operations:

import "log"

func main() {
	Pre()

	context := new(Context)
	child, _ := context.Reborn()

	if child != nil {
		PostParent()
	} else {
		defer func() {
			if err := context.Release(); err != nil {
				log.Printf("Unable to release pid-file: %s", err.Error())
			}
		}()

		PostChild()
	}
}

# Packages

# Functions

ActiveFlags returns flags that has the state 'set'.
AddCommand is wrapper on AddFlag and SetSigHandler functions.
AddFlag adds the flag and signal to the internal map.
BoolFlag returns new object that implements interface Flag and has state 'set' when var with the given address is true.
CreatePidFile opens the named file, applies exclusive lock and writes current process id to file.
Flags returns flags that was added by the function AddFlag.
NewLockFile returns a new LockFile with the given File.
OpenLockFile opens the named file with flags os.O_RDWR|os.O_CREATE and specified perm.
ReadPidFile reads process id from file with give name and returns pid.
SendCommands sends active signals to the given process.
ServeSignals calls handlers for system signals.
SetSigHandler sets handler for the given signals.
StringFlag returns new object that implements interface Flag and has state 'set' when var with the given address equals given value of v.
WasReborn returns true in child process (daemon) and false in parent process.

# Constants

Default file permissions for log and pid files.
Mark of daemon process - system environment variable _GO_DAEMON=1.
Mark of daemon process - system environment variable _GO_DAEMON=1.

# Variables

ErrStop should be returned signal handler function for termination of handling signals.
ErrWouldBlock indicates on locking pid-file by another process.

# Structs

A Context describes daemon context.
LockFile wraps *os.File and provide functions for locking of files.

# Interfaces

Flag is the interface implemented by an object that has two state: 'set' and 'unset'.

# Type aliases

SignalHandlerFunc is the interface for signal handler functions.