peek is designed to be script-friendly. This document shows how to use its JSON outputs and peekd history in automation, CI, and monitoring.
peek 1234 --json > process.jsonThis prints a single JSON object representing the current ProcessInfo for PID 1234. Example (fields trimmed):
{
"pid": 1234,
"name": "nginx",
"ppid": 1,
"cmdline": "nginx: worker process",
"rss_kb": 123456,
"vm_size_kb": 987654,
"threads": 12,
"cpu_percent": 42.3,
"fd_count": 128,
"network": {
"listening_tcp": [ /* ... */ ],
"connections": [ /* ... */ ]
}
}Use jq to extract fields:
peek "$PID" --json | jq '.cpu_percent'
peek "$PID" --json | jq '{pid, name, rss_kb, cpu_percent}'peek 1234 --json-snapshot > snapshot.jsonThis wraps ProcessInfo with capture metadata:
{
"captured_at": "2026-03-03T12:34:56Z",
"peek_version": "1.0.0",
"process": {
"pid": 1234,
"name": "nginx",
"rss_kb": 123456
/* ... */
}
}Useful for storing time-series snapshots in logs or object storage.
peekd exposes history over a Unix socket as JSON. In most cases you should call it via peek --history, but you can also talk to it directly:
printf '{"action":"history","pid":1234}\n' | socat - UNIX-CONNECT:/run/peekd/peekd.sockThe response is a JSON object with a data.samples array (one per sample with timestamp, CPU, memory, etc.).
In shell scripts:
CPU=$(peek "$PID" --json | jq '.cpu_percent // 0')
if [ "$(printf '%.0f' "$CPU")" -gt 80 ]; then
echo "High CPU for $PID: $CPU%"
fiPID=$(pgrep -n my-test-helper)
FD_BEFORE=$(peek "$PID" --json | jq '.fd_count // 0')
# run expensive tests here...
FD_AFTER=$(peek "$PID" --json | jq '.fd_count // 0')
if [ "$FD_AFTER" -gt "$((FD_BEFORE + 50))" ]; then
echo "File descriptor leak detected for $PID ($FD_BEFORE -> $FD_AFTER)" >&2
exit 1
fiIn your CI job:
if ! cargo test; then
PID=$(pgrep -n my-server || true)
if [ -n "$PID" ]; then
peek "$PID" --json-snapshot > peek-snapshot.json
# Upload peek-snapshot.json as a CI artifact.
fi
exit 1
fi- Prefer
--json-snapshotwhen you care about capture time and tool version. - Use
jq -rto get raw values without quotes for shell conditions. - Combine
peek --jsonwith systemdExecStartPostor cron jobs for lightweight health checks.