Skip to content

Process Tracker

Yousef Ghadiri edited this page May 30, 2026 · 4 revisions

Process Tracker

When enabled with --pid, Knightwatch tracks a root process and its entire child tree in real time, exposing snapshots via the /process family of endpoints. It also emits lifecycle events to the Telegram bot and webhook dispatcher.

knightwatch --pid <PID>

Pass --pid multiple times to track more than one root process simultaneously:

knightwatch --pid <PID1> --pid <PID2>

Endpoints

Endpoint Description
GET /root_pids List of all PIDs currently being tracked
GET /process/<PID> Full snapshot — root info, child processes, and work_done flag
GET /process/root/<PID> Root process snapshot only, or 404 if it has exited
GET /process/children/<PID> Snapshots of all currently live child processes
GET /process/status/<PID> Lightweight summary — alive/dead, child count, and work_done flag
GET /top-processes Top N processes sorted by CPU or memory (requires --top-processes)
GET /supported-signals Signals supported on the current platform

Process Commands

When the server is started with --allow-process-commands, the following write endpoints become available. These endpoints are not read-only — they actively affect running processes.

Authentication required. All process command endpoints always require authentication, regardless of whether --enable-auth is set. The auth session endpoints are automatically enabled alongside --allow-process-commands.

knightwatch --pid <PID> --allow-process-commands

Command Endpoints

Endpoint Description
POST /process/kill/<PID> Send a signal to a specific process
POST /process/kill-tree/<PID> SIGKILL a root process and its entire descendant subtree
POST /process/track/<PID> Begin tracking a new root PID
POST /process/untrack/<PID> Stop tracking a root PID and discard its state
POST /process/poll/pause Pause the polling loop
POST /process/poll/resume Resume the polling loop
POST /process/poll/interval Change the polling interval

Killing a Process

Send a signal to a single process by PID:

POST /process/kill/1234
Content-Type: application/json
 
{ "signal": "term" }

Available signals depend on the platform (see /supported-signals):

Signal Value Description
Kill kill Forceful termination (supported on all platforms)
Interrupt int Interrupt (Unix only)
Stop stop Pause execution (Unix only)
Continue cont Resume a stopped process (Unix only)
Terminate term Graceful termination request (Unix only)

On Windows, only kill is supported. Sending any other signal returns 400.

Killing a Process Tree

Sends SIGKILL to a root process and every process in its descendant subtree. Returns the list of PIDs that were successfully signalled:

POST /process/kill-tree/1234
[1234, 1235, 1236]

Tracking and Untracking

Track a new root PID at runtime without restarting the server:

POST /process/track/5678

Stop tracking a root PID and discard its accumulated state:

POST /process/untrack/5678

Both return 200 OK. Tracking an already-tracked PID is a no-op.

Controlling the Poll Loop

Pause snapshot collection (the tracker still responds to queries and commands):

POST /process/poll/pause

Resume at the current interval:

POST /process/poll/resume

Change the polling interval (takes effect immediately):

POST /process/poll/interval
Content-Type: application/json
 
{ "interval_ms": 2000 }

Process States

The state field on a process snapshot can be running, sleeping, gone, or any other platform string. Unknown states are rendered as a warning-colored pill in the dashboard.


Work-Done Detection

The work_done flag is set to true when all child processes of a tracked root have exited. The dashboard shows a completion banner at this point. The root process itself may still be running.


Webhook Events

Event Description Key data fields
process.initial_snapshot First capture after startup root, children, child_count
process.children_appeared New child processes detected pids, children
process.children_exited One or more children exited pids
process.all_children_gone All children have exited root_pid, root_name
process.root_exited Root process exited pid, name
process.work_complete Work-done condition met root_pid, root_name

Telegram Notifications and Commands

  • 🟢 Initial snapshot — root and child count when tracking begins
  • 🆕 Children appeared — new child PIDs detected
  • 🔴 Children exited — specific child PIDs exited
  • All children gone — all child processes have exited
  • 💀 Root process exited — the root process itself has stopped When --allow-process-commands is enabled, the Telegram bot also supports process commands (kill, track, untrack, poll control). Authentication is always required for these commands, even via Telegram.

Clone this wiki locally