Daemon
pg daemon registers and runs arbitrary long-lived commands directly on a Linux
host — a script, a FastAPI/uvicorn webhook receiver, a sync agent, or anything
else that should stay in the background. There is no container involved: the
process is owned by pgcli, tracked with a pid file and can be wired into the
existing boot service.
Registration and starting are separate steps: pg daemon add only records the
command, pg daemon start launches it. This keeps pg.yaml declarative and
lets a command be staged before it is first run.
Only Linux is supported.
How it works
- Each daemon has an entry in
pg.yamland a state directory under<base>/daemon/<name>/. pg daemon addresolves the executable to an absolute path, so starting at boot does not depend onPATH. Arguments are stored verbatim.pg daemon startlaunches the process withsetsidand records the pid; the process survives thepgcommand exiting. When rotation is enabled, a small supervisor process forwards stdout/stderr to the rotating log instead (see Log rotation).- Liveness is checked against
/proc/<pid>/cmdline, so a recycled pid is not mistaken for the daemon. - Stopping signals the whole process group:
SIGTERM, wait up to--timeout, thenSIGKILL.
State layout (when --log-file is not set):
| Path | Contents |
|---|---|
<base>/daemon/<name>/daemon.log |
stdout and stderr (append; rotated when configured) |
<base>/daemon/<name>/daemon.pid |
<pid> and the recorded binary path |
<base>/daemon/<name>/supervisor.log |
rotation supervisor diagnostics (only with rotation) |
<base> is base_dir from pg.yaml when set, otherwise the platform default
(usually ~/.pgcli).
Register a daemon
The daemon name and its command must be separated by --. Flags before --
configure pg daemon; everything after it belongs to the command:
| Flag | Default | Notes |
|---|---|---|
--dir |
current directory at add time |
Working directory of the process; must exist |
--log-file |
<base>/daemon/<name>/daemon.log |
Stored as an absolute path. A relative value resolves against --dir; parent dir is created on start |
--log-max-size |
disabled | Rotate when the log exceeds this size, e.g. 10M, 1G |
--log-rotate-daily |
false | Rotate at local midnight |
--log-keep |
7 | Rotated files to retain; 0 keeps all. Requires one of the rotation triggers |
--force |
false | Replace an existing registration; the daemon must be stopped |
Notes:
-
A bare command name (
uvicorn) is looked up inPATHat registration time and stored as an absolute path;--diris not part of that lookup. A command containing/— including./run.shand../bin/x— is resolved against--dirat registration time, and must exist and be executable. -
Arguments are stored verbatim and interpreted at run time with the working directory set to
--dir, so relative paths in arguments work as you would expect:A relative path in a bare command name is not resolved this way — use
./namewhen the executable lives in--dir. -
--log-file: relative paths are resolved against--dir— the same directory the process runs in.--log-file logs/hook.logwith--dir /opt/webhookwrites to/opt/webhook/logs/hook.log; an absolute value is used as-is. -
Keep one path style. Write every path — the command, its file arguments, and
--log-file— either all as absolute paths, or all relative to--dir. Mixing the two makes it easy to lose track of which directory a given path resolves against. -
Prefer the absolute path of an interpreter or virtualenv binary. A shebang such as
#!/usr/bin/env python3depends on the environment the process inherits, which differs between an interactive shell and the boot service. -
--forcekeeps the existingautostartflag. It refuses to replace a daemon that is currently running — stop it first. -
Daemon names use the same rule as SQLite databases:
[A-Za-z0-9][A-Za-z0-9_-]{0,47}.
Start and operate
startis idempotent. If the recorded process is already alive it prints its pid and does nothing.- After launching,
startwaits about half a second. If the process is already gone — bad interpreter, import error, port in use — it fails and prints the last 20 log lines, so the cause is visible immediately. stopaffects the whole process group, so a wrapper script that spawned a worker does not leave the worker behind. It does not change theautostartflag, matchingpg stopfor instances.statusshows state, pid, command, working directory, log path, rotation settings and autostart flag;listprints one table row per daemon.
Log rotation
A long-running receiver should not grow its log without bound. Enable rotation when the daemon is registered:
- At least one trigger,
--log-max-size SIZEor--log-rotate-daily, is required. Passing only--log-keepis an error. SIZEaccepts512K,10M,1G, a byte count, and so on; it is rounded up to whole megabytes because that is the rotation library’s granularity.--log-keep Nretains theNnewest rotated files. The default is7;0keeps all of them.- Both triggers can be combined; whichever fires first rotates the file.
Rotated files live next to the active log and are named
<base>-YYYY-MM-DDTHH-MM-SS.mmm<ext>: daemon.log produces files such as
daemon-2026-11-02T17-35-24.123.log. The active log always keeps the configured
path, so pg daemon logs and external collectors can keep reading it.
Rotation is lazy. The size is checked on every write, so the file may slightly exceed the limit before the next write triggers a rotation. A date boundary is applied on the first write of the new day — an idle daemon does not rotate until it writes again. If the daemon is restarted after midnight, the previous day’s file is moved aside before the first new line is appended. Timestamps use local time.
With rotation enabled, pgcli keeps a small supervisor process
(pg daemon __supervise <name>) between the pid file and the command. It
forwards stdout/stderr and owns the log file; this is what allows rotation to
happen while no pg command is running. pg daemon stop still signals the
whole process group, so the supervisor and the command exit together.
Supervisor diagnostics are written to <base>/daemon/<name>/supervisor.log.
Two side effects of rotation:
- Newly created log files have mode
0600(without rotation the log is created0644). Adjust permissions if another user must read it. pg daemon logs <name> -fswitches totail -F, so following continues in the new file after a rotation.
Remove
remove stops the process and deletes the entry from pg.yaml. If autostart
was enabled, the boot service is refreshed or removed when no targets remain.
--clean-log also deletes <base>/daemon/<name>/ (the default log, rotated
backups, pid file and supervisor log). A custom --log-file is left untouched
and reported; its rotated siblings stay next to it as well.
Auto-start on boot
pg daemon integrates with the existing pgcli boot service:
--name is required; there is no default daemon name. At boot,
pg start --autostart starts every registered daemon whose autostart flag is
true, after all instances and addons, so a webhook or agent can dial
databases, queues and reverse-proxy backends that are already up.
The boot path calls the same idempotent start logic: a daemon that is already running is left alone, and one that exits immediately is reported with its log tail while the rest of the boot pass continues.
Limitations worth planning around:
- Autostart starts the process once. It does not supervise or restart it if it later crashes. Run the command under its own supervisor if you need crash recovery.
- The process inherits the environment of the boot service, not your interactive login shell. Put required variables in the command’s own wrapper or configuration instead of relying on shell profiles.
- If pgcli itself runs as a different user under the boot service than during
pg daemon add, the state directory (<base>/daemon/<name>/) must be readable and writable by that user.
See Auto-start on Boot for the systemd/launchd service details.
Webhook receiver example
A complete, minimal FastAPI receiver that appends Alertmanager-style payloads to a log file:
/opt/webhook/app.py:
Register and run:
Send a test alert:
Expected response:
Check both the receiver log and the application payload log:
Troubleshooting
daemon ... exited immediately — pg daemon start already prints the log
tail. Run pg daemon logs <name> -n 100 for more context. Common causes are a
missing Python dependency, a wrong --dir, or a port that is already in use.
command not found in PATH — the bare command name was not visible to the
shell that ran pg daemon add. Use an absolute path or install the binary
where the current environment can see it, then re-add with --force.
The daemon starts but cannot see my shell variables — at boot the process
runs under the systemd user service, which has a different environment. Export
configuration in the command’s script rather than in ~/.bashrc.
daemon "x" is running; stop it before --force — stop it with
pg daemon stop x, then re-add.
Limits
- Linux only.
- One process per daemon entry; no supervision, backoff or restart policy.
- Logs are appended forever unless rotation is configured; use
--log-max-sizeand/or--log-rotate-dailyfor long-running receivers. - Stopping is process-group based. A daemon that deliberately escapes its
process group (for example with its own
setsid) may leave children behind. - Registration captures the command line, not a shell. Shell syntax such as
pipes or
&&must be executed explicitly with/bin/sh -c '...'.