Laptop computer displaying programming code on screen
Image: Tirza van Dijk via Wikimedia Commons (CC0)

The Midnight Training Run That Never Finished

I learned this one the hard way. A few weeks back, I kicked off a model fine-tuning job on my Linux workstation at the office — figured I’d let it run overnight and check the results in the morning. Walked in the next day, coffee in hand, ready to see some fresh loss curves. The screen was dark. Machine had gone to sleep two hours into the run.

Two hours. Out of an estimated six. All that GPU time, wasted. The checkpoint I thought would save me? Never got written because the process got SIGSTOP’d before the save interval hit.

If you’ve ever run a long AI agent task — a multi-hour coding session with an autonomous agent, a model fine-tune, a batch inference job, or even a big data processing pipeline — you’ve probably hit the same wall. Your OS, doing exactly what it was designed to do (save power), pulls the plug on your work.

If you’ve done any work with AI coding assistants or autonomous agents, you know the frustration. I covered how to build your own AI agent in Python — the same principle applies here: unattended processes need safeguards.

Here’s how to make sure that never happens again — on Mac, Linux, and Windows.

Why Your Computer Sleeps in the First Place

Modern operating systems are aggressive about power management. By default, macOS puts the display to sleep after 10 minutes and the system after slightly longer. Linux distributions vary — Ubuntu Server stays awake indefinitely, but Ubuntu Desktop has GNOME’s power settings kicking in. Windows 11 is particularly aggressive with its Modern Standby, which can suspend processes even when the lid is open.

The core issue: most power management treats “inactivity” as “no keyboard or mouse input.” Your AI agent churning through tokens, your training loop consuming 100% GPU — the OS sees none of that as “activity.” It’s just a process running in the background, and background processes don’t keep machines awake by default.

Here’s the fix for each platform.

macOS: The caffeinate Command Is Your Best Friend

macOS ships with a built-in tool called caffeinate that’s been there since Mountain Lion (10.8). It’s dead simple and covers almost every use case.

The Quick One-Liner

Prefix any command with caffeinate and your Mac stays awake until that command finishes:

caffeinate -i python3 train_model.py

The -i flag prevents idle sleep — exactly what you need for a background process with no user interaction. Your Mac will stay awake until train_model.py exits, then return to normal power management.

For Background Processes You Don’t Want to Baby-Sit

If you’re running something in the background and you want to walk away:

caffeinate -i -d python3 long_agent_task.py &

The -d flag also prevents the display from sleeping, which is helpful if you want to glance at the terminal output as you walk by. Drop the -d if you just need the CPU/GPU running and don’t care about the screen.

Timed Caffeination

Need your Mac awake for exactly 3 hours?

caffeinate -i -t 10800

That’s 10,800 seconds (3 hours). Your Mac stays awake for that duration, then goes back to normal. Useful if you roughly know how long your job takes and don’t want the machine awake indefinitely after it finishes.

One thing to watch: caffeinate only works while the lid is open. If you close a MacBook lid, the system goes to sleep regardless — caffeinate can’t override the hardware lid sensor. For lid-closed operation (headless mode), you need the next tool.

Lid-Closed Operation with Adrafinil

A new open-source tool hit Hacker News recently called Adrafinil. Its specific use case: keep a lid-closed MacBook awake while AI agents work. It uses IOKit’s IOPMAssertionCreateWithName API to create a system-wide power assertion that survives lid closure — something caffeinate can’t do on its own.

Install it with Homebrew:

brew install kageroumado/tap/adrafinil

Then run:

adrafinil -- python3 my_agent.py

The Mac stays awake with the lid closed, the agent runs to completion, and the machine goes back to normal sleep behavior afterward. It’s niche but exactly right for the AI agent use case where you want to close the laptop and throw it in your bag while work continues.

Linux: systemd-inhibit and Friends

On Linux, the approach depends on whether you’re running a desktop environment or a headless server. Server distros (Ubuntu Server, Debian, RHEL) typically don’t suspend at all — no action needed. But desktop Linux is a different story.

systemd-inhibit (The Universal Linux Approach)

Modern Linux systems use systemd-logind to manage sleep. The systemd-inhibit command lets you take a “lock” that prevents sleep:

systemd-inhibit --what=idle --why="AI model training in progress" python3 train_model.py

This works across GNOME, KDE, XFCE — any desktop environment that respects systemd. The --what=idle flag specifically prevents idle-triggered sleep, and the --why string shows up in the systemd inhibitor list:

systemd-inhibit --list

You’ll see your inhibitor there with its description. Other processes can’t override it without root.

GNOME-Specific: Disable the Screensaver & Suspend

If you’re on stock Ubuntu, Fedora, or any GNOME-based desktop, you can temporarily disable suspend:

gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type 'nothing'
gsettings set org.gnome.desktop.session idle-delay 0

After your job finishes, restore the defaults:

gsettings reset org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type
gsettings reset org.gnome.desktop.session idle-delay

A cleaner approach: wrap it in a script that saves the current settings, applies the temporary ones, runs your job, then restores. I keep a stay-awake.sh script in my ~/bin for exactly this:

#!/bin/bash
# Save current settings
OLD_SLEEP=$(gsettings get org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type)
OLD_IDLE=$(gsettings get org.gnome.desktop.session idle-delay)

# Disable sleep
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type 'nothing'
gsettings set org.gnome.desktop.session idle-delay 0

echo "System will stay awake. Press Ctrl+C to stop and restore settings."
trap "gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type $OLD_SLEEP; gsettings set org.gnome.desktop.session idle-delay $OLD_IDLE; echo 'Settings restored.'" EXIT

# Run whatever was passed
"$@"

Save it, chmod +x ~/bin/stay-awake.sh, then run stay-awake.sh python3 my_agent.py. When the job finishes or you Ctrl+C, your original power settings come right back.

WSL on Windows: A Special Case

If you’re doing AI work inside WSL2 (like I do on my work laptop), the Linux tools won’t help — WSL doesn’t control the host’s power state. You need to prevent sleep at the Windows level.

The simplest method: run this from PowerShell (as Administrator) before starting a long WSL job:

powercfg /change standby-timeout-ac 0
powercfg /change hibernate-timeout-ac 0
powercfg /change monitor-timeout-ac 0

After your job finishes, restore the defaults:

powercfg /change standby-timeout-ac 30
powercfg /change hibernate-timeout-ac 180
powercfg /change monitor-timeout-ac 10

The 0 means “never.” The post-job values (30 minutes standby, 180 minutes hibernate, 10 minutes monitor) are reasonable defaults — adjust to your preference.

You can run these from inside WSL by calling PowerShell:

powershell.exe -Command "powercfg /change standby-timeout-ac 0"

Fair warning: this affects the entire Windows system, not just WSL. If you’re on a laptop running on battery, you probably don’t want to do this — stick to AC power for long AI runs.

Speaking of terminal tools — if you spend a lot of time in WSL or Linux, getting comfortable with auditing dependencies before they hit production will save you hours. The same “set it and forget it” mindset applies to long-running AI jobs.

Third-Party Tools Worth Knowing

Beyond the built-in commands, a few tools make sleep management even smoother:

Caffeine (macOS): A tiny menu bar app. Click the coffee cup icon to prevent sleep, click again to re-enable. Great for ad-hoc “don’t sleep while I step away” moments. Free on the Mac App Store.

Amphetamine (macOS): More powerful than Caffeine. You can set triggers — “stay awake while this app is running,” “stay awake for X hours,” “stay awake while on this Wi-Fi network.” Useful if you regularly run long tasks and want automation without remembering to run caffeinate every time.

Caffeine (Linux): A system tray icon for Linux desktops. Install with sudo apt install caffeine on Ubuntu/Debian. It adds a toggle to your panel — click to prevent screensaver and suspend.

powercfg (Windows): Already covered above, but worth mentioning that you can create named power plans and switch between them:

# Create a "Stay Awake" profile
powercfg /duplicatescheme 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
powercfg /changename <NEW_GUID> "AI Training Mode"

# Switch to it before a job
powercfg /setactive <GUID>

# Switch back to Balanced after
powercfg /setactive 381b4222-f694-41f0-9685-ff5bb260df2e

What to Use When: A Quick Decision Guide

Scenario Tool Command
Single command, macOS, lid open caffeinate caffeinate -i python3 train.py
Lid-closed macOS agent runs Adrafinil adrafinil -- python3 agent.py
Single command, Linux desktop systemd-inhibit systemd-inhibit --what=idle python3 train.py
Multiple commands, GNOME gsettings script Use the stay-awake.sh wrapper above
WSL2 + Windows host powercfg powershell.exe powercfg /change ...
Headless Linux server Nothing needed Servers don’t suspend by default
Quick ad-hoc, any platform System tray app Caffeine / Amphetamine / power plan toggle

One Last Tip: Combine With a Checkpoint Strategy

No matter how solid your sleep-prevention setup, machines can still go down — power outages, kernel panics, someone accidentally unplugging the wrong cable. Pair your stay-awake setup with aggressive checkpointing.

For ML training, save checkpoints every N steps, not every N minutes. A power loss an hour into a job that checkpoints every 500 steps is recoverable. One that checkpoints every 3 hours isn’t. Most training frameworks (PyTorch, TensorFlow, Hugging Face Trainer) have built-in checkpoint callbacks — set the interval to something conservative.

For AI agent tasks, have your agent log its progress to a file and write intermediate results. If the agent dies at step 47 of 50, you should be able to pick up from step 47, not start over. Some agent frameworks handle this natively; others need a bit of custom state management.

The stay-awake tools above prevent the most common failure mode (OS-initiated sleep), but they’re the first line of defense, not the only one.

If you found the command-line tricks in this guide useful, you might also like my deep dive on mastering Linux command-line text processing with awk — it covers another set of terminal skills that pair nicely with the automation workflows we just set up.

The Bottom Line

Your computer’s power management was designed for document editing and web browsing — not for 6-hour AI agent runs or overnight model training. The good news is that every major OS gives you the tools to override it, and they’re all simple command-line one-liners.

On macOS, caffeinate -i is all you need 90% of the time. On Linux, systemd-inhibit works universally. On Windows/WSL, powercfg handles it. And if you’re closing a MacBook lid while an agent works, Adrafinil fills the niche nicely.

Pick your platform’s tool, make it part of your long-running-job workflow, and you’ll never wake up to a dark screen and a dead training run again. Your GPU cycles — and your coffee — deserve better.

Filed under Tech & Gadgets
Last Update: June 28, 2026 by Felix AlterEgo
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Newest
Oldest Most Voted