gotsr

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: BSD-2-Clause Imports: 17 Imported by: 3 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

How it works (POSIX)

The background process holds an fcntl(2) write lock on <pidfile>.lock for its whole lifetime. The kernel releases the lock when the process exits, even if it is killed with SIGKILL. IsRunning, PID and Terminate ask the kernel who holds the lock instead of trusting the PID written in the PID file, which is informational only. So:

  • checking the status never sends a signal;
  • a stale PID file never causes an unrelated process to be signalled;
  • a second TSR() returns ErrAlreadyRunning and leaves the running process alone;
  • Terminate sends SIGTERM to the lock holder and returns once it has exited.

Limitations:

  • The lock file is never removed; it stays next to the PID file as a sentinel.
  • The PID file must be on a local filesystem, and all processes must share a PID namespace.
  • A relative PID file path is resolved against the working directory when New is called. Use an absolute path (WithPIDFile) if the program can be started from different working directories.
  • On Linux, Terminate signals the process through a pidfd, which rules out PID reuse. On other systems a very small window remains between the lock check and the signal.
  • Windows uses a different, experimental mechanism and does not lock the PID file.

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")
	ErrAlreadyRunning = errors.New("already 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". A relative path is resolved against the current working directory when New is called.

On POSIX systems, the running TSR process holds a lock on "<pidfile>.lock" for its whole lifetime, and IsRunning, PID and Terminate rely on that lock rather than on the PID written in the PID file, which is informational only. The lock file is never removed. Both files must reside on a local filesystem.

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. Called in the TSR process, it also releases the lock. Called in any other process, it removes the PID file only if it is stale, i.e. no running TSR process holds it.

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. On POSIX systems it returns ErrNotRunning if no process holds the lock on the PID file.

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
⌂ 换网址