Caffeinate your Mac to prevent it from sleeping

· Dev.to

Originally posted on my blog.

TL;DR

I'll show you how to use macOS's built-in caffeinate command to keep your Mac awake:

  • until you stop it
  • for a set amount of time, or
  • only while a long-running command is executing

No additional apps that you need to install, no menu bar junk, no "why is my Mac sleeping again?!" drama.

Why you'd care about caffeinate

If you've ever kicked off a long download, rsync backup, Docker build, video export, or "I'll just run this migration quickly" job… and then came back to find your Mac politely sleeping like an innocent kitten — you already know why this matters.

macOS has an official tool for this, and it's been sitting right under your nose:

caffeinate

It's basically the Terminal equivalent of "No, macOS, we are not done here."

Option #0: The simplest possible usage

Open Terminal and run:

caffeinate

That's it. Your Mac won't go idle-sleep as long as this process is running.

To stop it press Ctrl + C

Option #1: Keep the Mac awake for a fixed amount of time

Sometimes you just want to keep your Mac awake for 2 hours (and keep that Slack icon 🟢 hey, I don't judge), and then you'd pass in the -t parameter (which indicates seconds):

caffeinate -t 7200

Read full story at source