Daemonize

package module
v0.0.0-...-01baa19 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2023 License: MIT Imports: 6 Imported by: 0

README

Daemonize

A daemonization toolkit for golang processes

Usage

Import the package

package main

import "github.com/Millefeuille42/Daemonize"

Create a new Daemonizer

package main

import (
	"github.com/Millefeuille42/Daemonize"
	"log"
)

func main() {
	d, err := Daemonize.NewDaemonizer()
	if err != nil {
		log.Fatal(err)
	}
	defer d.Close()	
}

Start the process as a daemon

package main

import (
	"github.com/Millefeuille42/Daemonize"
	"log"
)
func main() {
	d, err := Daemonize.NewDaemonizer()
	if err != nil {
		log.Fatal(err)
	}
	defer d.Close()

	pid, err := d.Daemonize(nil)
	if err != nil {
		log.Fatal(err)
	}
}

This will start the program as a daemon, with the working directory set as system root and with a logger interface on syslog.

Like fork, Daemonize returns a non 0 PID if the process is the parent process (child's PID for success, 1 for error) if the process is the child process, it returns 0.

It is recommended to exit the parent process right after.

package main

import (
	"github.com/Millefeuille42/Daemonize"
	"log"
	"os"
)

func main() {
	d, err := Daemonize.NewDaemonizer()
	if err != nil {
		log.Fatal(err)
	}
	defer d.Close()

	pid, err := d.Daemonize(nil)
	if err != nil {
		log.Fatal(err)
	}
	if pid != 0 {
		// In parent process
		log.Print(pid)
		os.Exit(0)
	}
	// In child process
}

It is possible to add loggers thanks to various functions, example:

d.AddFileLogger("/path/to/log/file", os.Args[0], log.LstdFlags)

Logging is done via the d.Log function, it logs on the registered loggers and to syslog

d.Log(syslog.LOG_INFO, "Hello there")

The severity parameters is for syslog.

The d.Close function also catches panic events to log it on files and syslog.

The "minimal" client code is as follows:

package main

import (
	"github.com/Millefeuille42/Daemonize"
	"log"
	"os"
)

func main() {
	d, err := Daemonize.NewDaemonizer()
	if err != nil {
		log.Fatal(err)
	}
	defer d.Close()

	pid, err := d.Daemonize(nil)
	if err != nil {
		log.Fatal(err)
	}
	if pid != 0 {
		log.Print(pid)
		os.Exit(0)
	}

	err = d.AddTempFileLogger("/path/to/log/folder", "daemon_*.log", os.Args[0], log.LstdFlags)
	if err != nil {
		log.Fatal(err)
	}

	d.Log(Daemonize.LOG_INFO, "Hello there")
}

Definitions

Severity

Severity is an indicator of the purpose of the log message, it is linked to syslog Priority levels.

type Severity int

const (
    // LOG_DEBUG Useful data for debugging
    LOG_DEBUG Severity = iota
    // LOG_INFO Non-important information, considered to be the default level
    LOG_INFO
    // LOG_WARNING Rare or unexpected conditions
    LOG_WARNING
    // LOG_ERR Errors
    LOG_ERR
    // LOG_EMERG Fatal errors
    LOG_EMERG
)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Daemonizer

type Daemonizer struct {
	// SyslogWriter is the writer used to write on the syslog
	SyslogWriter *syslog.Writer
	// Loggers is a slice containing all registered loggers
	Loggers []*log.Logger
	// contains filtered or unexported fields
}

Daemonizer is a helper struct for "daemonizing" go processes

func NewDaemonizer

func NewDaemonizer() (*Daemonizer, error)

NewDaemonizer creates a new Daemonizer instance and creates a writer to syslog

func (*Daemonizer) AddFileLogger

func (d *Daemonizer) AddFileLogger(path, prefix string, flags int) error

AddFileLogger opens a writer to the file located at path and opens a logger on it, the adds it to the Loggers slice

func (*Daemonizer) AddLogger

func (d *Daemonizer) AddLogger(logger *log.Logger)

AddLogger adds a logger to the Loggers slice

func (*Daemonizer) AddTempFileLogger

func (d *Daemonizer) AddTempFileLogger(dir, pattern, prefix string, flags int) error

AddTempFileLogger opens a writer to the file located at path and opens a logger on it, then adds it to the Loggers slice. It uses the os.CreateTemp function to open files

func (*Daemonizer) Close

func (d *Daemonizer) Close() error

Close closes SyslogWriter and all files. It also catches panic() events to log it in SyslogWriter and Loggers

func (*Daemonizer) Daemonize

func (d *Daemonizer) Daemonize(args []string) (int, error)

Daemonize uses os.StartProcess to start itself as a daemon, like fork, returns 0 if in the child process, returns child process Pid if spawned, returns 1 if an error occurred in the parent process, unlike fork. the args parameters corresponds to the arguments passed to the child process the caller name is already added

func (*Daemonizer) HandleSignals

func (d *Daemonizer) HandleSignals(additionalHandler func() error)

func (*Daemonizer) Log

func (d *Daemonizer) Log(severity Severity, v ...any)

Log logs on SyslogWriter and Loggers, the severity is used for syslog

func (*Daemonizer) Sid

func (d *Daemonizer) Sid() int

Sid returns the daemons SID

type Severity

type Severity int
const (
	// LOG_DEBUG Useful data for debugging
	LOG_DEBUG Severity = iota
	// LOG_INFO Non-important information, considered to be the default level
	LOG_INFO
	// LOG_WARNING Rare or unexpected conditions
	LOG_WARNING
	// LOG_ERR Errors
	LOG_ERR
	// LOG_EMERG Fatal errors
	LOG_EMERG
)

Jump to

Keyboard shortcuts

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