Nov 26, 2026 DISPATCH // HARDWARE, CODE & PLATFORMS

Systemd Can Put Hard Limits on AI Agent Jobs

Systemd can cap an agent's memory and CPU use, then stop it after a set runtime. Sleep inhibition and folder triggers cover other cases without custom scripts.
Systemd Can Put Hard Limits on AI Agent Jobs Nerds Magazine © nerdsmagazine.com
Systemd Can Put Hard Limits on AI Agent Jobs © nerdsmagazine.com

A runaway agent can consume enough memory to freeze a Linux machine before the kernel ends it with an out-of-memory error. Systemd offers controls to contain that kind of short-lived job.

The project is best known for booting Linux and running background services, but its features extend to resource limits and automated triggers. Systemd is Linux's service manager. Its resource controls cover services and scopes, as well as slices, not only long-running daemons. They are Linux-only.

Set limits on agent runs

MemoryMax and CPUQuota apply the same cgroup limits used by Docker. A command such as systemd-run --user --scope -p MemoryMax=4G -p CPUQuota=200% claude gives Claude Code a 4 GiB memory ceiling and up to two CPUs' worth of CPU time. These cgroup limits apply to the managed scope and its child processes, not just the foreground agent process. The Linux cgroup hierarchy provides the process-grouping mechanism. MemoryMax sets a hard memory boundary, while CPUQuota throttles aggregate CPU use. The resource control documentation describes the settings. To see usage aggregated by cgroup, use systemd-cgtop instead of top.

These process limits operate at a different layer from the Linux graphics-memory changes covered in this related Linux report.

A separate runtime cap can stop a job after 30 minutes. RuntimeMaxSec=30m sets a wall-clock limit, measured by elapsed time rather than CPU time. Unlike a CPU quota, it places an upper bound on how long the unit can run. To have OpenCode fix tests without running indefinitely, use systemd-run --user --same-dir --unit=opencode-agent -p RuntimeMaxSec=30m ~/.opencode/bin/opencode run --standalone --auto "Fix the failing tests in this repo and summarise what you changed", then follow it with journalctl --user -u opencode-agent -f. The --standalone option matters because OpenCode otherwise defaults to a background server that bypasses the limit. The --user form runs the job under the per-user systemd manager.

Automate jobs and protect a laptop

If a laptop closes during a long run, systemd-inhibit can request a lock against sleep. Whether the lock prevents the related to the topic sleep action depends on the desktop and logind configuration, inhibitor mode, and whether that action honors the lock. Pair it with a runtime limit so the agent does not run without a bound: systemd-run --user --same-dir --unit=claude-agent -p RuntimeMaxSec=30m systemd-inhibit --what=sleep:idle:handle-lid-switch --why="claude agent run" ~/.local/bin/claude -p "Fix the failing tests in this repo and summarise what you changed". The systemd-inhibit documentation describes inhibitor locks.

Systemd timers can run missed tasks after sleep or shutdown with the Persistent option. Their OnSuccess and OnFailure settings can route results to Slack or email units. Separately, RandomizedDelaySec can spread requests from multiple machines scheduled for 1 a.m. Timers can also send dead-man's-handle pings to Healthchecks.io.

Path units respond to folder changes. The systemd.path man page documents the triggers. For example, create agent-inbox.path with DirectoryNotEmpty=%h/agent-inbox and connect it to a service using OnSuccess=slack-notify.service. Set the service to Type=exec with RuntimeMaxSec=30m and MemoryMax=4G, then point ExecStart at the command to run. Dropped files can trigger an agent to categorize expenses or invoices as they arrive.

Topics:
Developer Tools Tech Explainers #AI Coding Assistants #AI Agents #Operating System Management & Utilities
Ethan Cole Editor Nerds Magazine
Editor

Ethan Cole

Ethan Cole is an Editor at NerdsMagazine covering software, Windows, gaming, PC hardware, and practical troubleshooting. He focuses on clear explanations, dependable fixes, and helping readers understand when a problem can be solved without unnecessary upgrades or replacement.