Skip to content

How it works

Stuck headless Claude Code runs and the deadline fix

Published · 5 min read·

A robot types at a desk while its paper scroll spills across the floor and a large hourglass runs low; a second robot slips a bookmark into the scroll and a third holds out a fresh blank page.

In short

  • `claude -p` has no flag that limits a whole run's time: a supervisor has to stop it, and Claude Code exits with code 143.
  • Each of my runs is a systemd unit with `RuntimeMaxSec` set to its deadline, `MemoryMax=8G`, `TasksMax=4000` and `KillMode=control-group`.
  • Before the deadline, the run is asked to hand off at 90 minutes and to wrap up in its last 15, so the next run continues the work.
  • 1 lead run in 1,461 has hit the 3-hour deadline so far, and a fresh run finished its issue in 73 minutes.

A headless Claude Code run never exits when the model never decides it is finished, because claude -p has no limit on a whole run's time. The fix is a deadline from outside: a supervisor that stops the run when its time is up. In Issuemill, every run is a systemd unit with RuntimeMaxSec set to its deadline, plus a memory limit and a process limit, and the run is asked to hand its work over before the cut comes.

That deadline wraps every run of Claude Code working unattended on my GitHub issues. A lead run gets 3 hours. Here is why, and how often it has mattered (spoiler: once).

Why does a headless run hang?

Claude Code limits each piece of a run, and never the run itself. A Bash command times out after two minutes by default and ten at most. Background commands in a -p run get 10 minutes. When the model is done, Claude Code stops leftover background work "after 10 minutes of continuous idle waiting" (headless docs and tools reference, checked 2026-12-01).

--max-turns has "No limit by default" (CLI reference, checked 2026-12-01), and no flag limits wall-clock time. The docs leave that to "a process supervisor": send SIGTERM, and Claude Code "exits with code 143" (headless docs, checked 2026-12-01).

So a run that keeps finding one more thing to do keeps doing it. My one real case was not even a frozen process. It was an agent working very hard on the wrong side of done: an implementer subagent made 745 tool calls in under 3 hours, 702 of them Bash, about 92 million input tokens over 724 API calls, with three silent gaps of 10 minutes each. It never ended on its own. Busy and stuck look the same from outside, and they cost the same.

What does a deadline do that a retry does not?

A retry starts the same run again. If the first one never ends, you now have two that never end, and a cron entry happily adds a third.

A deadline ends the run, frees its slot for the next issue, and leaves a mark. In my queue, a run still going at its deadline ends as timeout, which counts as a failed attempt on its issue. The first failure queues the issue again. The second one marks it blocked, which means it asks me. So a run that can't finish gets one more try with fresh eyes, then a human, and never an endless loop.

Which systemd settings do the work?

Every run starts with systemd-run --user as a unit of its own. These are the real lines from the code that builds the command:

`RuntimeMaxSec=${spec.deadlineSec}`,
`-p`,
`TimeoutStopSec=${STOP_TIMEOUT_SEC}`,
`-p`,
`KillMode=control-group`,
`-p`,
`TasksMax=${TASKS_MAX}`,
`-p`,
`MemoryMax=${MEMORY_MAX}`,

What each one does for me:

  • RuntimeMaxSec is the deadline. systemd stops the unit once it has run that long (systemd.service, checked 2026-12-01). A lead run gets 10,800 seconds, 3 hours. A planning run gets 20 minutes.
  • KillMode=control-group kills every process the run started, so a dev server or a test watcher it left behind dies with it instead of holding a port all night.
  • MemoryMax=8G is "the absolute limit on memory usage of the executed processes in this unit" (systemd.resource-control, checked 2026-12-01). A run that eats memory meets the kernel inside its own unit, far from my desktop.
  • TasksMax=4000 limits the processes and threads, so a fork loop stays inside the run.
  • TimeoutStopSec is 780 seconds: the time the unit gets after the stop to clean up.

The run also starts from an empty environment (/usr/bin/env -i) plus the variables it needs, so a stray token in my shell never reaches it.

If you run claude -p from a cron entry, you get the same effect from systemd-run --user -p RuntimeMaxSec=3h -p KillMode=control-group claude -p "...". What matters is that something outside the model holds the clock. If you would rather rent the machine per run, I compared that with my own server.

What happens to the work when the deadline hits?

A cut that throws away three hours of work would be a bad fix. So the run hears about the deadline long before it comes:

  • At 90 minutes, a lead run is advised to hand off: park the issue with a handoff note and let a fresh run continue.
  • In its last 15 minutes, any run with a deadline longer than 30 minutes is told to wrap up. The message says: "Start no new work. Commit and push the work in progress, if any, comment on the issue … with what is done and left, and end with status parked and a full handoff." It ends with the time the run is cut.
  • If it is still going at the deadline anyway, systemd stops it. A run that ends as timeout without a handoff gets its unpushed branches pushed for it, within 10 minutes, and the handoff gets a section that lists them. That is what the 780 seconds of TimeoutStopSec are for.

My one cut run shows the whole cycle. It started at 21:42 UTC and was cut at 00:43, after 3 hours and $30.16 at API prices. The next run on that issue started about 10 minutes later, ended done 73 minutes after that for $14.58, and its pull request merged. Same issue, a fresh run, less than half the price.

How it works shows where the deadline sits among the queue, the review and the limits.

How often does it happen to me?

Rarely, and I want to be straight about that. Since 6 Nov 2026:

  • 1 lead run in 1,461 was cut by the 3-hour deadline. The rewritten engine has cut none so far.
  • 5 planning runs hit their 20-minute deadline.
  • 67 lead runs went past 90 minutes, and 15 past 150. All but the one above ended before the cut.
  • 245 of 1,661 run units touched the 8G memory line at their peak. None was killed for it. All 13 out-of-memory kills on the machine happened inside test containers, outside any run.
  • No run came near 4,000 processes.

So the deadline is a smoke detector. It went off once in 1,461 lead runs, and that once it turned a busy agent with no end in sight into a fresh run that finished the issue in 73 minutes. I keep the battery in.

Get the next post by email

A short email when a new post is out or something ships. Confirm once by email, unsubscribe in one click.

All posts