{"modulePath":"github.com/MiniMax-AI/OpenAgentCore","version":"v0.0.14","isLatest":true,"isStandardLibrary":false,"goos":"linux","goarch":"amd64","docs":"# package daemonize\n\nPackage 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.\n\nRe-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.\n\n## Constants\n\n```go\nconst BackgroundSentinelEnv = \"OAC_RUNTIME_DAEMON_BACKGROUND_CHILD\"\n```\nBackgroundSentinelEnv 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.\n\n## Variables\n\n```go\nvar ErrNotRunning = errors.New(\"daemonize: process not running\")\n```\n\n```go\nvar ErrStaleOrCorrupt = errors.New(\"daemonize: unsupported, stale or corrupt process record\")\n```\n\n## Functions\n\n```go\nfunc EnsureLogFile(path string) error\n```\nEnsureLogFile 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\".\n\n```go\nfunc IsBackgroundChild() bool\n```\nIsBackgroundChild 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.\n\n```go\nfunc NotifyContext(parent context.Context) (context.Context, context.CancelFunc)\n```\n\n```go\nfunc ReadPIDFile(path string) (int, error)\n```\n\n```go\nfunc RemovePIDFile(path string) error\n```\n\n```go\nfunc Spawn(argv []string, opts ReExecOptions) (int, error)\n```\nSpawn 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.\n\nOn error the partially-opened log file is closed and a best-effort pidfile cleanup runs so a half-spawn doesn't leave stale state.\n\n```go\nfunc StopPIDFile(path string, timeout time.Duration) error\n```\nStopPIDFile never removes ownership before the exact process has exited. A cleanup timeout leaves the record available for observation and a later stop.\n\n```go\nfunc Tail(path string, opts TailOptions, ctxDone \u003c-chan struct{}, w io.Writer) error\n```\nTail 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.\n\nPass a nil ctxDone when Follow is false.\n\n## Types\n\n```go\ntype ReExecOptions struct {\n\t// LogPath is the absolute log file path. Child stdin is\n\t// /dev/null; stdout+stderr are appended to this file (0o600).\n\tLogPath string\n\n\t// PIDPath is the absolute process-record path. The parent writes the\n\t// child identity here before returning; existing files are replaced\n\t// atomically.\n\tPIDPath string\n}\n```\nReExecOptions controls how Spawn launches the background child.\n\n```go\ntype TailOptions struct {\n\t// LastLines is the number of trailing lines to print before any\n\t// follow logic kicks in. 0 → print nothing historical, jump\n\t// straight to follow.\n\tLastLines int\n\n\t// Follow keeps the tail open after printing LastLines, polling\n\t// for new bytes appended by a still-running daemon. false →\n\t// return immediately once the historical chunk is flushed.\n\tFollow bool\n\n\t// PollInterval is the cadence at which Tail re-stats the file\n\t// looking for growth. Zero → 500ms.\n\tPollInterval time.Duration\n}\n```\nTailOptions configures Tail.\n\n","path":"github.com/MiniMax-AI/OpenAgentCore/apps/daemon/internal/daemonize","name":"daemonize","synopsis":"Package daemonize gives `oac-daemon connect -b` a no-cgo way to detach from the controlling terminal on macOS, Linux and Windows.","isRedistributable":true}
