How to keep builds and coding agents running when you leave your Mac

You start a long job (a coding agent on a real task, a full test run, a big build), go for lunch, and come back to find it did four minutes of work and then waited for fifty-six. Almost always, one of three things happened. Each has a simple fix.

1. Your Mac went to sleep

A sleeping Mac runs nothing: not the job, not its tests, not the network connection it depends on. macOS puts a Mac to sleep after it's been idle for a while, and a MacBook sleeps as soon as you close the lid.

Keep it awake for one job: caffeinate

caffeinate ships with macOS. Put it in front of a command, and the Mac stays awake until that command finishes:

caffeinate -i npm run build

-i prevents idle sleep. Add -s to also prevent system sleep while the Mac is on power, or -d to keep the display on. To cover something that's already running, point caffeinate at its process ID:

caffeinate -i -w 48213

To see what's currently keeping your Mac awake (or not), run pmset -g assertions. Every flag is explained in caffeinate: keep your Mac awake from the terminal.

Keep it awake while plugged in: System Settings

The display can still turn off, which saves power, while the work carries on.

The lid

Closing a MacBook's lid puts it to sleep, and caffeinate doesn't change that. The supported exception is closed-display mode: the Mac is on power with an external display, keyboard and mouse connected. Some third-party utilities offer a lid-closed mode too; read their caveats about heat before using one in a bag. The simplest rule: if an agent is working, leave the lid open and the charger in.

2. The session died when you left

If the job runs in a terminal window and you reach it over SSH, it's tied to that connection. When your phone drops the connection, the program can be killed with it. Two ways around this:

For non-interactive jobs, nohup your-command > job.log 2>&1 & also works, but a job that asks questions needs a live terminal to ask them in. More options, from disown to launchd, in keeping a process running after you close the terminal.

3. The job is waiting for you

Coding agents ask before they edit files or run commands, and that's a feature. It does mean that an hour-long task can spend most of the hour waiting for a "y".

A setup that works

  1. Plug the Mac in and turn on the "prevent automatic sleeping" setting above, or run agents under caffeinate -i.
  2. Start the job in a session that outlives your connection: tmux, or a PandaSend session.
  3. Pre-approve the safe, repetitive actions; keep approval for anything destructive.
  4. Check in from your phone when you like, and answer when it asks.

None of this is complicated, but skipping any one of the three is how an afternoon of agent work turns into ten minutes.