Daemon Lifecycle
Manage the daemon through opta daemon. This guide covers lifecycle control, health instrumentation, and platform-native service registration for resilient startup behavior.
Starting the Daemon
The opta daemon start command launches the daemon as a background process. It writes its PID, port, and authentication token to the daemon state file under the Opta config root (macOS/Linux: ~/.config/opta/daemon/state.json, Windows: %APPDATA%\\opta\\daemon\\state.json).
If the daemon is already running, the command will report the existing process and exit without starting a second instance.
Checking Status
The opta daemon status command shows the current state of the daemon process, including PID, port, uptime, and active session count.
If the daemon is not running, you will see:
Viewing Logs
The daemon writes structured logs to the daemon log path (~/.config/opta/daemon/daemon.log on macOS/Linux and %APPDATA%\\opta\\daemon\\daemon.log on Windows). Use the logs subcommand to tail the file:
Restarting
The daemon CLI currently exposes explicit stop and startcommands. For a clean restart, run stop then start.
Stopping
opta daemon stop sends a graceful shutdown signal, waits for clean termination, and force-stops only when necessary. Active sessions remain preserved on disk and can be resumed after restart.
Health Check
You can verify the daemon is responding to requests using the health endpoint:
Auto-Start on Boot
The opta daemon install command registers the daemon as a system service so it starts automatically when you log in.
launchd (macOS)
On macOS, the install command creates a launchd plist at ~/Library/LaunchAgents/com.opta.daemon.plist.
The generated plist configures the daemon to:
- Start at login (
RunAtLoad) - Restart on crash (
KeepAlive) - Log stdout/stderr to
~/.config/opta/daemon/
systemd (Linux)
On Linux, the install command creates a systemd user service at ~/.config/systemd/user/opta-daemon.service.
schtasks (Windows)
On Windows, the install command registers a Scheduled Task that launches the daemon at user logon.
Uninstalling
The opta daemon uninstall command removes the system service registration and stops the daemon if it is running.
Crash Recovery
If the daemon crashes unexpectedly, the CLI detects the stale state file on the next command and automatically restarts it. The crash guardian in ensureDaemonRunning performs the following steps:
Detect stale PID
The CLI reads state.json and checks whether the PID is still alive using a signal-zero check.
Clean up state file
If the PID is dead, the stale state.json is removed.
Restart daemon
A new daemon process is spawned with a fresh token and state file.
Retry the original command
The CLI retries connecting to the new daemon and proceeds with the user's command.