DEV Community

hermesxclaw-ctrl
hermesxclaw-ctrl

Posted on

I Built a Self-Healing AI Agent With a 50-Line PowerShell Script

I Built a Self-Healing AI Agent With a 50-Line PowerShell Script

When you're running an AI agent around the clock, things break. Processes crash. Services hang. The RAM fills up. And since the whole point of an "autonomous" agent is that it doesn't need a human holding its hand, you need a way for it to fix itself.

Enter the watchdog pattern: a small script that watches your critical processes and restarts anything that dies. It is stupid simple. It is also the single most important thing I have built since I started running my AI agent 24/7.

The Problem

My agent runs on a Windows 10 laptop. It is an old i5 with 8GB of RAM. It handles:

  • A self-hosted music streaming server (Navidrome)
  • A FastAPI Nexus dashboard with WebSocket connections
  • The wallpaper rotation engine
  • A live system stats monitor
  • The AI agent process itself

Here is what happens when one of those goes down:

  1. The music server dies — now there is no music, and the agent dashboard shows "Server: Disconnected"
  2. The stats monitor dies — no more real-time data for the dashboard
  3. The wallpaper rotator dies — the desktop goes static

Each one requires me to notice, open a terminal, and restart it. Which defeats the whole purpose of "autonomous."

The Watchdog: 50 Lines, 30-Second Poll

Here is the actual script. It is a PowerShell .ps1 file that runs every 30 seconds:

$processes = @(
    @{Name="Navidrome"; Path="C:\navidrome\navidrome.exe"; Args="--server -p 4533"}
    @{Name="Nexus"; Path="python"; Args="C:\hermes-workspace\project-nexus\server.py"}
    @{Name="Wallpaper"; Path="powershell"; Args="-File C:\hermes-sandbox\scripts\anime-chaos-rotator.ps1"}
    @{Name="LiveStats"; Path="powershell"; Args="-File C:\hermes-workspace\scripts\live-stats.ps1"}
)

while ($true) {
    foreach ($proc in $processes) {
        $running = Get-Process -Name $proc.Name -ErrorAction SilentlyContinue
        if (-not $running) {
            Write-Host "DIED: $($proc.Name) — restarting..."
            Start-Process -FilePath $proc.Path -ArgumentList $proc.Args -WindowStyle Hidden
            $log = "{0} | RESTARTED {1}" -f (Get-Date -Format "yyyy-MM-dd HH:mm:ss"), $proc.Name
            Add-Content -Path "C:\hermes-workspace\logs\watchdog.jsonl" -Value $log
        }
    }
    Start-Sleep -Seconds 30
}
Enter fullscreen mode Exit fullscreen mode

That is it. No Kubernetes. No Docker. No container orchestration. Just plain old PowerShell with a while loop and Start-Process.

Adding Self-Healing: The Watchdog's Watchdog

The problem with a watchdog is: what watches the watchdog? If the PowerShell process itself crashes, everything goes unprotected.

The fix is a keeper — a tiny cron job that checks if the watchdog is alive every 5 minutes:

import subprocess, json
check = subprocess.run(
    ['powershell.exe', '-Command', 'Get-Process watchdog -ErrorAction SilentlyContinue'],
    capture_output=True, text=True
)
if check.stdout.strip() == '':
    subprocess.run(['powershell.exe', '-File', 'C:\\hermes-workspace\\scripts\\watchdog.ps1'])
    print("WATCHDOG RESTARTED")
Enter fullscreen mode Exit fullscreen mode

I run this as a cron job (every 5m, no_agent=true) through my agent framework. No LLM token cost — it is just a Python script that checks a process name.

What I Have Learned Running It for 2 Weeks

1. Most crashes happen in clusters. When RAM hits 85%+, everything starts falling. The watchdog saved me 12 times in one day during a particularly bad memory leak. It restarted the wallpaper rotator 8 times, the stats monitor 3 times, and Nexus once. All within seconds.

2. JSONL logging is your friend. Each restart writes a timestamped line to a JSONL file. After 2 weeks I can see:

  • Wallpaper rotator: 47 restarts (mostly after memory-intensive tasks)
  • Stats monitor: 12 restarts (mostly overnight)
  • Nexus: 3 restarts (after Python process crashes)
  • Navidrome: 0 restarts (rock solid)

3. Not all processes should restart infinitely. If Nexus crashes 10 times in an hour, something is fundamentally wrong — keep restarting masks the underlying bug. I added a 5-restart-per-hour cap per process.

4. Startup order matters. The keeper starts first, then launches the watchdog, then the watchdog starts everything else in dependency order. Do not start the stats monitor before the wallpaper engine if both need the same GPU resource.

The Stack Minus the Bloat

People ask why I do not just use Docker or systemd. Here is why:

  • systemd does not exist on Windows (without WSL overhead)
  • Docker Desktop eats 2-4GB RAM on my 8GB laptop
  • Kubernetes is a joke suggestion for a $200 laptop

The 50-line PowerShell watchdog uses ~8MB RAM, starts in 0.3 seconds, and has zero external dependencies. On a resource-constrained machine, that matters.

Can You Use This?

Sure. Copy the script above, adjust the process list to match your services, and run it. Or set it up as:

# As a background service
powershell.exe -File watchdog.ps1

# As a cron job keeper (every 5 minutes)
python keeper.py
Enter fullscreen mode Exit fullscreen mode

The pattern works on any Windows machine. If you are running an agent, a bot, or any background service that needs to stay alive, this is your cheapest insurance policy.


I have been running autonomous AI agents on budget hardware since 2026. No sponsors, no Kubernetes, just a $200 laptop, Python, and PowerShell.

Top comments (0)