Tools
System Settings, caffeinate, pmset
Price
Free (built into macOS)
Time
5 min
Level
Beginner

You start Claude Code on a long job: sorting a folder of invoices, rewriting the product pages of a shop, transcribing a batch of videos. You close the screen and leave, and the Mac goes to sleep. The next morning, the agent stopped at line three. The Mac is not broken: it is saving its battery, as designed. This tutorial shows how to keep it working with the tools Apple provides, and what not to ask of it.

The three ways a Mac sleeps

A Mac can stop in three ways, and each setting only acts on one of them:

  • The display turns off: harmless, programs keep running.
  • Idle sleep: nobody touches the keyboard, so the Mac sleeps. This is what stops most agents started in the evening.
  • Closing the lid: on a laptop, closing the screen puts the Mac to sleep. The Claude Code documentation says so for its scheduled tasks: even with the option that keeps the computer awake, closing the lid still puts it to sleep.

The Apple setting, for a Mac on power

  1. On a MacBookOpen System Settings, then Battery, click Options and turn on "Prevent automatic sleeping on power adapter when the display is off". The display turns off and the Mac keeps going, as long as it is plugged in.
  2. On a desktop Mac (iMac, Mac mini, Mac Studio)System Settings, then Energy, and turn on "Prevent automatic sleeping when the display is off". This is the simplest option if a Mac mini acts as a small server for your automations.

The caffeinate command, only for the length of the job

The setting above applies all the time. If you would rather keep the Mac awake only during one specific job, macOS ships the caffeinate command. It creates an "assertion", a request to the system not to sleep, and drops it when it stops.

  1. Wrap the agent's commandPut in front of a command, caffeinate keeps the Mac awake exactly as long as that command runs. -i prevents idle sleep, -s prevents system sleep (valid on AC power only, according to the manual). An example with Claude Code in non-interactive mode:
    Terminal
    caffeinate -is claude -p "Read every PDF in the invoices/ folder and write to invoices.csv: date, supplier, total including tax."
  2. Or for a fixed durationRun it in one Terminal tab while the agent works in another. -t sets the duration in seconds (here 4 hours); Ctrl + C stops it earlier.
    Terminal
    caffeinate -is -t 14400
  3. Check what is preventing sleepThis command lists the programs keeping the Mac awake. caffeinate should appear in it.
    Terminal
    pmset -g assertions

Lid closed: closed-display mode

Apple documents only one way to keep a laptop working with the lid closed: closed-display mode. Its help page on external displays asks for the Mac to be connected to power and used with an external keyboard and mouse.

  1. Plug in the chargerOn battery, closing the lid puts the Mac to sleep.
  2. Connect an external display, keyboard and mouseConnect the display directly to a port on the Mac if you can.
  3. Close the lidWork continues on the external display. You can then turn that display off: the "Prevent automatic sleeping…" setting above takes over.

Tricks that promise to keep a MacBook awake with the lid closed and no external display rely on options the pmset manual does not document. I do not recommend them: a closed laptop in a bag heats up and cannot breathe.

An example in a small business

A three-person agency wants Claude to prepare, every night, a summary of the requests received during the day. The sturdiest setup: an old Mac mini plugged in somewhere, set to "Prevent automatic sleeping…", running a scheduled task from the Claude desktop app. That app also offers a Keep computer awake option in Settings, This computer, System. Its documentation states that a scheduled task is skipped if the computer is asleep at the scheduled time. And if the task does not need your files, Claude Code routines run in the cloud, with the Mac switched off.

Limits, cost and security

  • Lock the screen before you leave with Ctrl + Cmd + Q: programs keep running, but nobody can use your session. Avoid caffeinate -d, which keeps the display on.
  • An agent running all night uses tokens: on an API key, every hour is billed. Give it a precise instruction and a clear end point, then check /usage in the morning.
  • No permission prompts at night: in -p mode, the agent cannot ask you for permission. Run the task once during the day to see what needs allowing, and only give it access to the folder it needs.
  • Battery and heat: keep the laptop on power, flat, with air around it.

Worth knowing. A Mac that never sleeps stays reachable on the network and uses electricity. Keep these settings for the nights an agent is actually working, and turn them off afterwards.

Sources

Want an automation that runs on its own every night, on a Mac or a server? That is the kind of automation I set up.