brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: caffeinate stops your Mac from sleeping while a long command runs

Mac
~/tips/tip-macos-caffeinate

caffeinate -i rsync -a ~/Photos /Volumes/backup/

Keeps your Mac awake only while one long command runs, then lets it sleep again.

A big download, a long rsync or a slow export is exactly when a MacBook decides to take a nap. macOS ships a small command for this, and I like it more than any menu bar app because it protects one specific job and then lets go.

caffeinate creates a power assertion, which is macOS’s way of saying “something is busy, please do not sleep”. When the command ends, the assertion disappears and the Mac behaves normally again. Nothing to switch back, and nothing to forget about on a Friday evening.

Put caffeinate in front of the command you care about:

caffeinate -i rsync -a ~/Photos /Volumes/backup/

The Mac stays awake until rsync exits, whether it finishes or fails. Without a command, caffeinate keeps going until you press Ctrl+C.

  • -i prevents idle sleep of the system. This is the one you usually want for a job in the terminal.
  • -d keeps the display awake as well.
  • -s prevents sleep while on AC power only.
  • -t 3600 stops after 3600 seconds, with or without a command.
  • -w <pid> waits for a process that is already running. Handy when you forgot to start it with caffeinate.

For a job that is already running, look up its process id with pgrep -x rsync and attach to it:

caffeinate -i -w $(pgrep -x rsync)

It does not override closing the lid on battery power. A MacBook with the lid shut sleeps unless it is on power with an external display attached (clamshell mode). On a laptop, also remember that keeping it awake in a bag is how you end up with a hot machine and a flat battery.

There is also a permanent route: pmset can change the sleep settings permanently, but that is a system-wide choice, and I prefer the temporary, per-command approach of caffeinate. Check man caffeinate for the complete list.

Further reading: caffeinate man page (macOS), On this site: pbcopy puts the output of any command on your Mac clipboard, On this site: pmset -g assertions shows what keeps your Mac awake, On this site: Guide: keep your Mac apps in a Brewfile, so a new MacBook is one command away

Going deeper: Guide: keep your Mac apps in a Brewfile, so a new MacBook is one command away

More Mac tips 4. Hold Shift and click Continue in Safe Mode 1. Shut down the Mac System Settings > Lock Screen > Show message when locked (on) Modifier key for scroll gesture: Control Allow in the Background (switches) All Mac tips (9) All topics

Link