Keep computer awake command generator

Choose how long the computer should stay awake and copy the command: caffeinate on macOS, systemd-inhibit on Linux. Each flag is explained, along with the caveats that catch people out.

Computer
Keep it awake

caffeinate flags (macOS)

caffeinate creates a power assertion and holds it for as long as it runs. The flags choose which kind of sleep it holds off.

FlagWhat it does
No flagsPrevents idle sleep, the same as -i.
-iPrevents the system from idle sleeping.
-sPrevents the system from sleeping. Valid only when the Mac is running on AC power.
-dPrevents the display from sleeping.
-mPrevents the disk from idle sleeping.
-uDeclares that the user is active. Turns the display on if it is off. Lasts 5 seconds unless you add -t.
-t secondsDrops the assertion after that many seconds. Not used when caffeinate wraps a command.
-w pidHolds the assertion until the process with that ID exits. Ignored when caffeinate wraps a command.
caffeinate cmdRuns the command and holds the assertion until it finishes.

systemd-inhibit options (Linux)

systemd-inhibit takes an inhibitor lock from logind, runs a command, and releases the lock when the command exits. It is built around running a command, which is why the generator uses sleep for the time-based choices.

OptionWhat it does
--what=Colon-separated list of what to inhibit: shutdown, sleep, idle, handle-power-key, handle-reboot-key, handle-suspend-key, handle-hibernate-key, handle-lid-switch. Default idle:sleep:shutdown.
--who=A short description of the program taking the lock. Defaults to the command line.
--why=A short reason. Defaults to "Unknown reason".
--mode=block (the default) stops the operation with no time limit; delay only postpones sleep or shutdown briefly; block-weak is like block but not enforced against root or the lock's owner.
--listLists active locks instead of taking one.

Caveats

  • Battery. caffeinate -s only applies on AC power. On battery use -i, and expect the battery to be the limit.
  • MacBook lid. Apple lists closing the display as a way to put a Mac laptop to sleep. Its documented closed-lid setup needs power plus an external display, keyboard and mouse. Otherwise keep the lid open.
  • Linux laptop lid. logind's LidSwitchIgnoreInhibited defaults to yes, so a sleep lock does not stop a lid-close suspend. Add handle-lid-switch, which is always honoured.
  • It lives as long as the process. If you start it over SSH and log out, it can end with the session. Run it in a tmux or Herdr pane, or use nohup.
  • Permissions on Linux. Taking an inhibitor lock is a privileged operation checked by polkit, so a system can refuse it for your user.
  • It cannot wake anything. These commands keep an awake computer awake. Melta, our phone app for coding agents over SSH (for iPhone and Android), cannot wake a sleeping computer either, so set this up before you leave. For the permanent System Settings switch and what Wake-on-LAN can do, see how to wake a Mac remotely and keep it awake for SSH.

Common questions

How do I keep my Mac awake from the Terminal?

Run caffeinate -i. The Mac will not idle sleep until you stop the command, or add -t 3600 to release it after an hour.

Does caffeinate work with the MacBook lid closed?

Do not count on it. Apple lists closing the display as a way to put a Mac laptop to sleep, and documents closed-lid use only with power, an external display, and an external keyboard and mouse.

Does caffeinate -s work on battery?

No. The manual says the -s assertion is valid only when the system is running on AC power. Use -i to hold off idle sleep on battery.

What is the Linux equivalent of caffeinate?

systemd-inhibit. For example, systemd-inhibit --what=sleep:idle sleep 7200 blocks suspend and idle handling for two hours, and replacing sleep 7200 with your own command holds the lock while that command runs.

Why does my Linux laptop still suspend when I close the lid?

By default logind ignores sleep and idle locks for the lid switch. Add handle-lid-switch to --what, or change HandleLidSwitch in logind.conf, which defaults to suspend.

How do I check that caffeinate is working?

Run pmset -g assertions in another terminal. It prints a summary of the power assertions currently held. On Linux, systemd-inhibit --list shows the active locks.