Language
November 26, 20263 min read

Prevent Mac Sleep While Claude Code Runs (caffeinate, 2026)

Start Claude Code with caffeinate -i claude. The -i flag stops your Mac from idle sleeping while the session runs.

If a session is already running, attach caffeinate to it with caffeinate -i -w <pid>. Both commands are below. If you want to check on the agent from your couch, Macky puts your Mac's terminal on your iPhone.

Start Claude Code under caffeinate

Put caffeinate in front of the command you normally type:

caffeinate -i claude

Caffeinate runs Claude Code for you and holds a no-idle-sleep assertion on its behalf. When you quit Claude Code, caffeinate exits and normal sleep rules come back. The binary lives at /usr/bin/caffeinate, so there's nothing to install.

Add -d if you also want the display to stay on: caffeinate -d -i claude.

Keep an already running session awake

Open a second Terminal window and point caffeinate at the process ID (PID) of the running session. Activity Monitor shows the PID column.

caffeinate -i -w 12345

Replace 12345 with your PID. The -w pid flag waits for that process to exit, then caffeinate stops on its own.

Check on Claude Code from your iPhone

Macky gives you a real shell on your Mac from your iPhone, so you can attach to the Terminal session where Claude Code is running. The connection is end-to-end encrypted WebRTC with no open ports and no VPN.

Claude Code running on a Mac, viewed in the Macky terminal on iPhone

The Mac app also has a keep awake switch and runs from the menu bar. You can leave caffeinate running as well. For other ways to use the agent away from your desk, see Claude Code on iPhone.

Which caffeinate flags to use

FlagWhat it does
-iStops the system from idle sleeping
-dStops the display from sleeping
-mStops the disk from idle sleeping
-sStops the system from sleeping, but only on AC power
-t secondsSets how long the assertion lasts (ignored when you run a utility through caffeinate)
-w pidWaits for the process with that PID to exit

For a long agent run, -i is the usual pick. Use -s only if the Mac stays plugged in. For a fixed window with no command, caffeinate -t 3600 holds the assertion for 1 hour. More examples are in caffeinate on Mac.

Closing the lid is a different case

We couldn't confirm how caffeinate behaves with the lid closed, so don't count on it for that. Closing the lid is a separate case from idle sleep. Follow keeping a MacBook awake with the lid closed for that setup.

FAQ

How do I prevent my Mac from sleeping when Claude Code is running?

Run it under caffeinate: caffeinate -i claude. The Mac won't idle sleep while Claude Code is running, and the assertion ends when you quit.

Can I keep my Mac awake for a Claude Code session that's already running?

Yes. Use caffeinate -i -w <pid> with the process ID of the running session. It holds the assertion until that process exits.

How do I stop caffeinate?

Press Ctrl+C in the Terminal window where caffeinate is running. If you wrapped Claude Code, quitting it also ends the assertion.

Does caffeinate -s work on battery?

It only works while the Mac is on AC power. On battery, -s doesn't hold the assertion, so use -i instead.

Does caffeinate keep a Mac awake with the lid closed?

That isn't confirmed here, so don't rely on caffeinate alone. Macky's Mac app has a closed-lid mode and a keep awake switch. See the lid-closed guide linked above for the full setup.

Try Macky

Connect to your Mac terminal from your iPhone.