Prevent Mac sleep during a long terminal command
Scripts, large rsync transfers, builds, and data jobs in the terminal can easily run longer than the idle sleep timer allows. Here is how to prevent Mac sleep during a long terminal command, including where the built-in caffeinate fits and where a safety-aware approach helps.
Why long commands get cut off
A command in Terminal runs as a normal process tied to your Mac's power state. Step away long enough to hit the idle timer, or close the lid, and the Mac sleeps with the command suspended.
Long-running scripts, big rsync transfers, and data jobs are the ones most likely to outlast the idle timer. That is exactly when a sleep interruption costs you, since these are the commands you cannot quickly redo.
The annoyance is that the command was working fine. It did not fail, the Mac just went to sleep underneath it.
caffeinate, and where it stops
macOS ships caffeinate for this, and it is genuinely useful. Running caffeinate, or wrapping a command with caffeinate -i your-command, holds the system awake for the duration and releases when the command exits. For a quick keep-awake on a single job, it is the right tool and costs nothing.
What caffeinate does not do is watch the battery or the heat. It will keep a laptop awake until the charge runs out, and it has no notion of thermal state on a long, hot job.
LidRun's CLI takes the same single-command idea and adds those guardrails: lidrun -- your-command holds a short-lived keep-awake assertion just for that run and releases it when the command exits, while the app's battery and thermal limits still apply.
When to reach for which
For a five-minute script on a plugged-in Mac, caffeinate is perfectly fine and you do not need anything more.
For a long unattended job, especially on battery or with the lid closed, the battery floor and thermal watch are what keep a multi-hour command from draining the Mac flat or running hot unsupervised.
If your terminal jobs regularly run long while you are away, it is worth trying LidRun so a single command, or a whole session, runs inside those safety limits.
A practical setup
For a one-off, prefix it: lidrun -- your-long-command, and let it release the assertion when the command finishes.
For a longer session, plug in, start your command, turn on keep-running mode, then close the lid if you want to step away. Set a session timer so a script that hangs does not hold the Mac awake forever.
Keep the Mac on a hard, ventilated surface for the heavy jobs, since a long build or compress can run warm.
LidRun keeps your work running with the lid closed, with battery and thermal safety built in.
Frequently asked
caffeinate reliably keeps the Mac awake for a command, but it does not watch battery or heat. For a long unattended job, a tool that adds those limits is safer.
Yes. The lidrun CLI holds a keep-awake assertion for the duration of a single command and releases it when the command finishes, with battery and thermal limits still applying.
Yes, with keep-running mode on, as long as battery and thermal state stay within the thresholds you set.
A session timer caps the run at one, three, or eight hours, so a hung command does not keep the Mac awake indefinitely.