gotsr

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2023 License: BSD-2-Clause Imports: 13 Imported by: 1

README

GO-TSR

gotsr — is a library for daemonising Go programs.

TSR stands for Terminate-and-Stay-Resident, was an acronym describing the type of behaviour when loaded program would return the control to the Operating System, and continue to execute in memory. These types of programs commonly refer to daemons in Unix/Linux systems and Services in Windows.

Currently only POSIX systems are supported (tested to work on Linux, Darwin and NetBSD). Windows support is experimental.

For usage example, see cmd/responder

Documentation

Overview

Package TSR provides the API to make the program run in the background, what used to be called "Terminate and Stay Resident" back in the days.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoPID      = errors.New("PID unknown")
	ErrNotRunning = errors.New("not running")
)

Functions

func SetLogger

func SetLogger(l Logger)

SetLogger sets the logger for the package. If not set, the package will be silent. The default logger is a nilLogger. If TSR is initialised with with WithDebug(true) option, the default logger will be set to a standard Go logger.

Types

type Logger

type Logger interface {
	Print(v ...interface{})
	Printf(format string, v ...interface{})
	Println(v ...interface{})
}

type Option

type Option func(*Process)

func WithDebug

func WithDebug(b bool) Option

func WithPIDFile

func WithPIDFile(fullpath string) Option

type Process

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

func New

func New(opts ...Option) (*Process, error)

New returns new Process. If caller does not set the PID file path and name explicitely with WithPIDFile option, it is inferred from the executable file name. So that the PID file for "foo.exe" will be "foo.pid".

func (*Process) AtExit

func (p *Process) AtExit(fn func())

AtExit appends the function to the list of functions that will be executed when the TSR process terminates. It should be called before TSR() is called.

func (*Process) Close

func (p *Process) Close() error

Close removes the PID file.

func (*Process) IsRunning

func (p *Process) IsRunning() (bool, error)

IsRunning returns true if the TSR process is running.

func (*Process) PID

func (p *Process) PID() (int, error)

PID returns the PID of the TSR process if it's running.

func (*Process) TSR

func (p *Process) TSR() (headless bool, err error)

TSR starts the program in the background.

func (*Process) Terminate

func (p *Process) Terminate() error

Terminate instructs the TSR process to terminate if it's running.

Directories

Path Synopsis
cmd
responder command

Jump to

Keyboard shortcuts

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