TIL How to Run a Script Without Mac Sleeping

Have you ever started a long-running script on your Mac, only to come back and find it stopped because your MacBook went to sleep? TIL a simple yet useful built-in macOS command that solves this problem.

It’s called caffeinate — a command-line utility located at /usr/bin/caffeinate that prevents your Mac from sleeping while a process runs.

How to Use caffeinate

There are several options available, but the one I use most often looks like this:

caffeinate -di some_long_running_script.sh

caffeinate -di bin/slow_executable

tl;dr: just prepend caffeinate to your shell script, command, or executable to prevent your Mac from sleeping while it runs.

Here’s what those flags mean:

  • -d — Prevent the display from sleeping
  • -i — Prevent the system from idle sleeping
  • -m — Prevent the disk from idle sleeping
  • -s — Prevent system sleep while on AC power
  • -t <seconds> — Set a timeout for the assertion

Using -di together keeps both your Mac’s display and system awake until your command finishes. I use this when I run full test suite locally.

What Happens When It Ends?

Once the caffeinate command completes or is stopped, your Mac returns to its normal sleep schedule as configured in your “Energy Saver” preferences.

Now, I’m not the only one who can be caffeinated! ☕💻

Leave a Reply

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