daemonize

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 15 Imported by: 0 modules, 0 packages

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.

Documentation

Overview

Package daemonize gives `oac-daemon connect -b` a no-cgo way to detach from the controlling terminal on macOS, Linux and Windows. Strategy is re-exec-the-binary rather than POSIX double-fork: the parent opens connect.log + connect.pid, then starts a fresh copy of its own argv with stdio redirected to the log file and a sentinel env var set so the child skips the fork branch. Setsid puts the child into its own session so closing the user's shell doesn't kill the daemon.

Re-exec (not raw fork) because Go's runtime is not fork-safe — the goroutine scheduler holds locks the child can't release without exec. exec.Cmd.Start does fork+exec, which is the safe combination.

Index

Constants

View Source
const BackgroundSentinelEnv = "OAC_RUNTIME_DAEMON_BACKGROUND_CHILD"

BackgroundSentinelEnv is set by the parent on the child's environment so the child knows it's the post-fork incarnation and must NOT itself try to re-fork. runConnect inspects via IsBackgroundChild.

Variables

View Source
var ErrNotRunning = errors.New("daemonize: process not running")
View Source
var ErrStaleOrCorrupt = errors.New("daemonize: unsupported, stale or corrupt process record")

Functions

func EnsureLogFile

func EnsureLogFile(path string) error

EnsureLogFile creates the log file (0o600) if missing so a `oac-daemon logs` before the first `connect -b` gets "0 bytes" instead of "no such file".

func IsBackgroundChild

func IsBackgroundChild() bool

IsBackgroundChild reports whether this process was spawned by a `connect -b` re-exec. runConnect skips the fork branch when true, otherwise the child would spawn grandchildren forever.

func NotifyContext

func NotifyContext(parent context.Context) (context.Context, context.CancelFunc)

func ReadPIDFile

func ReadPIDFile(path string) (int, error)

func RemovePIDFile

func RemovePIDFile(path string) error

func Spawn

func Spawn(argv []string, opts ReExecOptions) (int, error)

Spawn re-execs the current binary in the background. argv is the new process's full argv including argv[0]. Spawn does NOT scrub `-b` — BackgroundSentinelEnv is what tells the child to skip the fork.

On error the partially-opened log file is closed and a best-effort pidfile cleanup runs so a half-spawn doesn't leave stale state.

func StopPIDFile

func StopPIDFile(path string, timeout time.Duration) error

StopPIDFile never removes ownership before the exact process has exited. A cleanup timeout leaves the record available for observation and a later stop.

func Tail

func Tail(path string, opts TailOptions, ctxDone <-chan struct{}, w io.Writer) error

Tail prints the last opts.LastLines lines of path to w, optionally following the file (poll-on-stat) until ctxDone fires. Designed for human eyes — not a high-throughput log aggregator.

Pass a nil ctxDone when Follow is false.

Types

type ReExecOptions

type ReExecOptions struct {
	// LogPath is the absolute log file path. Child stdin is
	// /dev/null; stdout+stderr are appended to this file (0o600).
	LogPath string

	// PIDPath is the absolute process-record path. The parent writes the
	// child identity here before returning; existing files are replaced
	// atomically.
	PIDPath string
}

ReExecOptions controls how Spawn launches the background child.

type TailOptions

type TailOptions struct {
	// LastLines is the number of trailing lines to print before any
	// follow logic kicks in. 0 → print nothing historical, jump
	// straight to follow.
	LastLines int

	// Follow keeps the tail open after printing LastLines, polling
	// for new bytes appended by a still-running daemon. false →
	// return immediately once the historical chunk is flushed.
	Follow bool

	// PollInterval is the cadence at which Tail re-stats the file
	// looking for growth. Zero → 500ms.
	PollInterval time.Duration
}

TailOptions configures Tail.

Jump to

Keyboard shortcuts

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