Caffeinate your Mac to prevent it from sleeping
Source: 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, 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
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
Pass the -t parameter (seconds) to specify a duration:
caffeinate -t 7200 # 2 hours
Common values:
| Duration | Seconds |
|---|---|
| 30 minutes | 1800 |
| 1 hour | 3600 |
| 2 hours | 7200 |
| 4 hours | 14400 |
Option #2: Keep the display awake as well
Use the -d switch to prevent the display from dimming:
caffeinate -d
Option #3: Keep Mac awake only while a command runs
Wrap your long‑running command with caffeinate -i:
caffeinate -i your_command_here
Examples
caffeinate -i rsync -av ~/Pictures/ /Volumes/Backup/Pictures/
caffeinate -i npm run build
caffeinate -i ./run-tests.sh
When the wrapped command finishes, caffeinate stops automatically—no cleanup required.
Option #4: Run caffeinate in the background
If you want to keep your Terminal usable:
caffeinate -d -i &
You can verify it’s running:
ps aux | grep caffeinate
Stop it
-
Bring it to the foreground and press Ctrl + C:
fg -
Or kill it by PID (shown in the
psoutput):kill
Handy flag cheat sheet
| Goal | Command |
|---|---|
| Keep system awake until stopped | caffeinate |
| Keep awake for 1 hour | caffeinate -t 3600 |
| Keep display awake | caffeinate -d |
| Keep awake while a command runs | caffeinate -i |
| Display + system, background | caffeinate -d -i & |
Bonus: a tiny alias (because typing is hard)
Add this to your ~/.zshrc (or ~/.bashrc):
alias awake='caffeinate -d -i'
Reload the shell configuration:
source ~/.zshrc
Now you can run:
awake rsync -av ~/Stuff/ /Volumes/Backup/Stuff/
…and your Mac stays awake while the command runs.
Hope this was useful! 💪