Keeping macOS Awake via CLI — caffeinate -dimsu
caffeinate is a macOS command-line utility that prevents the system from sleeping by creating activity assertions. It keeps the Mac awake during tasks, automation, or remote operations, ensuring processes continue uninterrupted even when the system would normally idle into sleep.
On macOS , power management is designed to conserve energy by putting the system, display, and storage devices into sleep states when idle. While this behavior is beneficial for everyday use, it can become a constraint in automation, long-running processes, and remote administration scenarios. The caffeinate utility provides a direct CLI mechanism to override these defaults by asserting system activity and keeping the machine awake for as long as required.
A commonly used invocation is:
$ sudo caffeinateThis alone keeps macOS awake for the duration of the command’s execution, or until the process is terminated.
For more granular control over what sleep behaviors are suppressed, caffeinate can be combined with flags that target specific power states:
$ sudo caffeinate -dimsuThese flags extend its behavior beyond basic system idling control, allowing precise suppression of display sleep, system idle sleep, disk sleep, and even simulating user activity -u when required.
What caffeinate Does
caffeinate is not a background service that keeps running like a daemon.
Instead, it’s just a normal command that starts a small process. While that process is running, macOS treats it as a signal to stay awake by setting “don’t sleep” rules in the system power manager.
When you stop the command (or the process ends), those rules are automatically removed, and macOS goes back to its normal sleep behavior.
Flag Breakdown
-d
— Display Sleep Prevention
Prevents the screen from turning off.
- Keeps UI session visible
- Useful during presentations or active monitoring
-i
— Idle System Sleep Prevention
Prevents the system from sleeping due to inactivity.
- Critical for long-running scripts
- Ensures SSH sessions don’t drop due to sleep
-m
— Disk Sleep Prevention
Prevents spinning down or idling storage devices.
- Important for large file transfers
- Reduces latency spikes in disk I/O pipelines
-s
— AC Power Sleep Lock
Prevents system sleep while plugged into power.
- Ensures stability for server-like usage on Mac hardware
- Common in lab environments or CI runners
- Important: this one is ignored on battery
- It only applies when power adapter is connected
-u
— Simulated User Activity
Injects synthetic user activity signals.
- Keeps macOS “thinking” a user is active
- Useful when GUI session must remain alive without interaction
PS:caffeinate doesn’t override battery mode — it competes with it.
macOS still has final authority over power management, especially on battery, but caffeinate raises a strong “stay awake” signal that usually wins for idle/sleep decisions.
Real-World Operational Scenario (CLI-Only Mac Access)
Situation: GUI is inaccessible, system is still reachable via SSH
Imagine the following production scenario:
- A Mac mini is deployed as a build node or media processing box
- The GUI session becomes unresponsive due to:
- WindowServer crash loop
- GPU driver hang
- Stuck login session after sleep/wake failure
- Screen sharing (VNC/ARD) fails or never recovers
- Physical access is unavailable (rack-mounted or remote site)
At this point:
- The machine is still reachable via SSH
- CPU, disk, and network stack are alive
- But macOS power management is aggressively attempting sleep transitions
Stabilization Strategy:
Running caffeinate in the Background
To maintain system wakefulness without blocking the active shell session, caffeinate is commonly executed as a background process:
$ sudo caffeinate -dimsu &Running caffeinate this way provides two key outcomes:
- The system is forced to remain awake across display, idle, disk, and activity states
- The terminal remains free for continued diagnostics, scripting, or recovery workflows
This effectively turns the Mac into a CLI-stable recovery node, even when the GUI subsystem is compromised.
Process Management:
Stopping a Background caffeinate Session
To stop a background caffeinate session (for example one started with &), you treat it like any other shell-managed process.
Typical approaches include:
1. Find the job in your shell
If you launched it in the current terminal:
$ jobsYou’ll see something like:
[1]+ Running sudo caffeinate -dimsu &The number in brackets is the job ID.
2. Bring it to the foreground (optional)
$ fg %1This moves the process back into the terminal so you can interact with it directly (useful if you want to stop it cleanly).
3. Stop it properly
Option A — Kill via job control
$ kill %1This sends a termination signal to the job.
Option B — Kill via process name (simplest)
$ sudo killall caffeinateThis forcefully stops all running caffeinate instances owned by your user/system context.
Option C — Kill via PID (more precise)
Find PID:
$ ps aux | grep caffeinateThen:
$ kill <PID>or force kill:
$ kill -9 <PID>Once caffeinate is terminated, macOS immediately releases the associated power management assertions and reverts to its default energy policy. From that point onward, the system resumes normal behavior—idle timers, display sleep, disk sleep, and system sleep are once again governed entirely by the configured power settings and user inactivity thresholds.
caffeinate -dimsu is a small but powerful macOS control mechanism that prevents unwanted sleep states across display, system, disk, and user-idle layers. In operational contexts—especially when GUI access is degraded or unavailable—it becomes a reliable way to keep a Mac stable and reachable via CLI during troubleshooting and recovery workflows.
We hope this was helpful.