Keep Your Mac Awake for Claude Code and Codex

A coding agent can work for an hour or more. If your Mac goes to sleep, the run pauses and can lose its connection. Here is how to keep it going, with the lid open or closed.

Written by Pratik Nayak, the developer of Stay Awake. This guide covers free built-in options as well as our app. Updated November 6, 2026.

If the lid stays open: caffeinate

macOS includes caffeinate, which stops idle sleep while a command runs. Start your agent through it and the Mac stays awake until the agent exits, then sleeps normally again:

caffeinate -i claude

Or keep the Mac awake for a fixed time from a separate Terminal tab, here two hours:

caffeinate -i -t 7200

The display can still turn off, which is fine for an agent. It is free and needs no install. Run man caffeinate for the other options.

Closing the lid is a different problem

caffeinate prevents idle sleep, but closing a MacBook's lid uses a separate sleep path. Without an external display, the Mac still sleeps when you shut it. Apple's supported way to work with the lid closed is closed-display mode, with an external display, power and a keyboard and mouse connected.

Apple's closed-display guidance ↗

Lid closed, no monitor: Stay Awake

Stay Awake's Keep Awake When Lid Closed mode keeps a MacBook working with the lid shut and no external display. It is built for exactly this kind of unattended task.

  1. Download the free trial and move StayAwake into Applications.
  2. Open the cup menu and choose an Auto-Stop time that covers the run: 10 minutes, 30 minutes, 1 hour or 3 hours.
  3. Select Keep Awake When Lid Closed and approve the helper once when prompted.
  4. Start your agent, then close the lid on a ventilated surface.

When the timer ends, normal sleep comes back on its own. Closed-lid mode also stops when macOS reports serious thermal pressure or the battery falls below your chosen level, 10% by default. If the app quits or crashes, its helper is designed to restore normal sleep.

Before you walk away

  • Approvals: an agent waiting for you to approve a command stops anyway. Check its permission settings before you leave it.
  • Network: keeping the Mac awake does not keep Wi-Fi in range. If the Mac moves, the agent can lose its connection.
  • Power: plug in for long runs. On battery, closed-lid mode stops at your cutoff.
  • Heat: use a ventilated surface, not an enclosed bag. Cutoffs are a backstop, not a guarantee against overheating.

Which should you use?

  • Lid open at your desk: caffeinate -i in front of your agent's command. Free and built in.
  • External display and power: Apple's closed-display mode.
  • Lid closed, no monitor, with a timer: try Stay Awake free for 7 days.

Compare Mac keep-awake apps ↗ · The full closed-lid guide ↗