Command-line Knowledge for MacOS: Preventing Sleep

One of the things I love about what I do is that I discover some oddity that makes me say to myself: “Self, Why did you not know about that?” Today’s entry in that category is MacOS’s caffeinate command. It’s a command-line tool that prevents your Mac from sleeping for a period of time, or while a specific process is running. It is simple, reliable, and does not require installing anything.


How caffeinate works

macOS has several power management behaviors:

  • Display sleep: the screen turns off.
  • System sleep: the whole Mac goes to sleep.
  • Disk sleep: disks can spin down.
  • Idle sleep: sleep that happens because there is no activity.

caffeinate creates a temporary “stay awake” assertion with the power management system. While the assertion is active, macOS will avoid the kind of sleep you told it to prevent.


The simplest usage

Start caffeinate in Terminal:

caffeinate

As long as that command is running, your Mac will be kept awake (until you stop it).

To stop it, press:

  • Ctrl + C

Common options (the ones you will actually use)

You can tailor what gets prevented:

  • -d prevents display sleep
  • -i prevents idle system sleep
  • -m prevents disk sleep
  • -s prevents system sleep (particularly useful on AC power)

Examples:

# Keep the Mac awake, but let the display sleep
caffeinate -i

# Keep the display awake (presentation mode)
caffeinate -d

# Keep system awake (often best for long tasks)
caffeinate -s

# Keep everything you can awake
caffeinate -dims

Note: Some flags make more sense in certain situations (for example, -s is most relevant when the Mac is plugged in).


Set a time limit with -t

If you want “stay awake” behavior for a fixed amount of time, use -t with a number of seconds.

# Stay awake for 1 hour
caffeinate -i -t 3600

A handy trick: if you think in minutes, multiply by 60.


Tie it to a command (the best way for long jobs)

Instead of manually starting and stopping caffeinate, you can run it while another command runs.

# Keep the Mac awake while rsync runs
caffeinate -i rsync -av ~/Source/ /Volumes/Backup/Source/

When the rsync command finishes, caffeinate exits automatically.

This pattern is great for:

  • Backups and file copies
  • Long builds
  • Data imports
  • Video exports
  • Large downloads

Real-world recipes

1) Keep your Mac awake while a large file downloads

If you are using curl:

caffeinate -i curl -LO "<https://example.com/bigfile.zip>"

2) Prevent sleep during a presentation

caffeinate -d

Leave it running for the duration of the presentation, then Ctrl + C.

3) Keep your Mac awake for a meeting, then stop automatically

# 90 minutes
caffeinate -i -t 5400

Safety notes and gotchas


  • Preventing sleep can use more power and generate more heat, especially on laptops.
  • If you use caffeinate with no time limit, it will run until you stop it. If you forget, your Mac may stay awake all night.
  • If the goal is “do not lock my screen,” that is a different setting. caffeinate is about sleep behavior, not password prompts.

Quick cheat sheet

caffeinate              # keep awake until you stop it
caffeinate -i           # prevent idle sleep
caffeinate -d           # prevent display sleep
caffeinate -i -t 600    # preventsleep for 10 minutes
caffeinate -i <command> # keep awake while command runs

Linux equivalent?

Many Linux desktop environments use the caffeine utility, which includes a caffeinate binary in some distributions (like Debian/Ubuntu) that mimics the macOS syntax.

Installation:

  • Ubuntu/Debian/Mint: sudo apt install caffeine
  • Arch Linux: sudo pacman -S caffeine-ng
  • Fedora: sudo dnf install caffeine-ng [1, 2]

Syntax & Usage:

  • Prevent screen blanking while running a command: bash caffeinate COMMAND Use code with caution.
  • Toggle via GUI: Run caffeine or caffeine-indicator from your app menu to get a coffee cup icon in your taskbar, then click it to toggle idle-prevention on and off manually

Closing thoughts

If you work in Terminal even occasionally, caffeinate is one of those small tools that saves you from big annoyances. The best default is usually caffeinate -i, and the best habit is adding it in front of any command you expect to run for a long time.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *