Skip to content

caffeinate on Mac: prevent sleep during deployments

The caffeinate command prevents macOS from sleeping during AWS deployments, Docker builds, and data migrations. Practical guide with real examples.

Updated on 12 May 2026

Your terraform apply has been running for ten minutes. You step away to refill your mug. When you come back, the screen is black, the Mac is asleep, and your CloudFormation stack is in a state nobody wants to debug on a Friday evening.

We lived this scenario at LCMH during an S3 data migration for an Alsatian e-commerce client. 47 GB of product media, an aws s3 sync kicked off at 5 PM, and a MacBook that decided to nap after 15 minutes of keyboard inactivity. Result: partial sync, manual file-by-file verification the next morning.

The fix is one word: caffeinate.

A native macOS command to block sleep

caffeinate has existed since OS X 10.8. It creates IOKit power management assertions that prevent the system from entering sleep. No app to download, no account to create, no menu bar clutter. Just the Terminal you already use for deployments.

Under the hood, caffeinate intercepts the same signals macOS uses internally when a video is playing or a video call is active. You’re telling the system: “I have work in progress, don’t sleep.”

Wrapping your AWS and Docker commands with caffeinate -i

The most relevant pattern for DevOps: prefix your long-running command with caffeinate -i. The Mac stays awake for exactly the duration of execution, then resumes normal behavior.

caffeinate -i terraform apply -auto-approve
caffeinate -i aws s3 sync ./build s3://my-production-bucket --delete
caffeinate -i docker build --no-cache -t my-app:latest .

The -i flag means “prevent idle sleep” — it blocks sleep triggered by user inactivity. When your command finishes (success or failure), caffeinate exits. No cleanup needed.

Our team systematically uses this pattern for serverless operations that chain packaging, S3 upload, and Lambda deployment. An sam deploy that takes 8 minutes should not be interrupted by a power-saving policy.

Keeping the Mac awake for a fixed duration

Sometimes you’re not wrapping a single command but monitoring a background process. The -t flag accepts a duration in seconds:

caffeinate -t 3600

One hour. The Mac stays awake, then resumes normal settings. Quick reference: 30 minutes = 1800, 2 hours = 7200, 4 hours = 14400.

We use this variant when monitoring CloudWatch during a progressive deployment: the terminal displays metrics, caffeinate -t ensures the screen stays on during the observation window.

Preventing display sleep with the -d flag

By default, caffeinate -i prevents system sleep but lets the display turn off. If you’re visually monitoring real-time logs, add -d:

caffeinate -d -i kubectl logs -f deployment/api-production

Combine both flags when you need to keep an eye on output without touching the keyboard.

Creating an alias for daily deployments

If you deploy multiple times a day, an alias in ~/.zshrc eliminates repetition:

alias awake='caffeinate -i'

After a source ~/.zshrc, you simply write:

awake ./scripts/deploy-staging.sh

Eleven fewer characters to type. Over a week of sprints, it adds up.

The -s flag for battery-powered deployments

Classic situation: you’re on the train from Mulhouse to Paris, using the travel time to push a hugo deploy to S3. On battery, macOS is more aggressive about sleep. The -s flag forces the system to stay awake even without AC power:

caffeinate -s -i hugo deploy --target production

Caveat: this drains the battery faster. Reserve this flag for short operations (under 30 minutes) when you’re unplugged.

When caffeinate isn’t enough: automate server-side

caffeinate solves a one-off problem. For critical, recurring deployments, we believe the right answer is a CI/CD pipeline that doesn’t depend on your local machine.

A GitHub Actions workflow or AWS CodePipeline runs your builds on a remote server. Your Mac can sleep, update, or crash — the deployment continues. This is the approach we systematically recommend for production.

caffeinate remains the ideal tool for ad hoc operations: a file transfer to a NAS, a database export before a migration, a local build you’re testing before pushing to CI. For anything that touches production on a recurring basis, infrastructure must be resilient regardless of your workstation.

Next time you kick off a long-running operation from your terminal, those eleven characters — caffeinate -i — will separate a successful deployment from a ruined Friday evening.

Frequently asked questions

Is caffeinate installed by default on macOS?
Yes, caffeinate has been built into macOS since OS X Mountain Lion (10.8). No additional installation is required.
Does caffeinate use significant system resources?
No. caffeinate uses kernel-level IOKit assertions. Its footprint is negligible: a few MB of RAM, no measurable CPU impact.
How do I prevent sleep only while a script is running?
Prefix your command with caffeinate -i. Example: caffeinate -i ./deploy.sh. The Mac stays awake during execution then resumes normal behavior.
What's the difference between caffeinate and an app like Amphetamine?
caffeinate is native, scriptable, and dependency-free. Third-party apps add a GUI useful for non-developers, but unnecessary in a DevOps context.
Does caffeinate work when the Mac is on battery?
By default, macOS may ignore certain assertions on battery. Add the -s flag to force the Mac to stay awake even without AC power.

Related Articles