DEV Community

Caffeinate your Mac to prevent it from sleeping

This blog post explains how to use macOS's built-in `caffeinate` command to prevent your Mac from sleeping. `Caffeinate` is a command-line tool that keeps your Mac awake, useful for long-running tasks. The simplest usage involves running `caffeinate` in the terminal to keep the system awake until manually stopped. You can also specify a duration with the `-t` parameter, keeping the Mac awake for a set time in seconds. The `-d` flag keeps the display from dimming. The most efficient method is to use `-i` to run a command while preventing sleep, ensuring the Mac stays awake only during that operation. This approach is ideal for tasks like backups, builds, and tests. Caffeinate can be run in the background using the `&` operator for terminal usability. You can view running caffeinate processes with `ps aux | grep caffeinate`. Stopping a background `caffeinate` instance involves bringing it to the foreground with `fg` and then using `Ctrl + C`, or killing it by its process ID. Finally, the post provides a helpful cheat sheet of common caffeinate uses and suggests creating an alias for easier usage. This alias, like `awake`, simplifies the command syntax, enhancing productivity.
favicon
dev.to
dev.to