# caffeinate — Prevent Your Mac from Sleeping

> Practical guide to caffeinate: keep your Mac and display awake and prevent idle sleep during long builds, downloads or presentations.

Source: https://www.jpkc.com/db/en/cheatsheets/macos/caffeinate/

<!-- PROSE:intro -->
caffeinate is a small but genuinely handy macOS command: it keeps your Mac awake and stops the display or the system from drifting into sleep. That is invaluable when a long build is running, a large download must not be interrupted, or you are giving a presentation and the screen keeps dimming. Short flags let you control exactly which kind of sleep is blocked – and you can wrap caffeinate around another command so the Mac stays awake only for as long as that task runs.
<!-- PROSE:intro:end -->

## Basic Usage

`caffeinate` — Prevent sleep until you press Ctrl+C.

```bash
caffeinate
```

`caffeinate -t <seconds>` — Prevent sleep for a specific duration.

```bash
caffeinate -t 3600
```

`caffeinate <command>` — Prevent sleep while a command runs.

```bash
caffeinate make build
```

`caffeinate -w <pid>` — Prevent sleep while a process is running.

```bash
caffeinate -w $(pgrep rsync)
```

## Sleep Assertion Types

`caffeinate -d` — Prevent the display from sleeping.

```bash
caffeinate -d
```

`caffeinate -i` — Prevent idle sleep (system stays awake).

```bash
caffeinate -i
```

`caffeinate -s` — Prevent system sleep (even when on AC power lid close is allowed).

```bash
caffeinate -s
```

`caffeinate -m` — Prevent disk from going idle.

```bash
caffeinate -m
```

`caffeinate -dims` — Combine multiple assertions (display + idle + disk + system).

```bash
caffeinate -dims
```

## Common Patterns

`caffeinate -i rsync -avz <src> <dest>` — Keep system awake during a large file transfer.

```bash
caffeinate -i rsync -avz ~/Photos/ /Volumes/Backup/Photos/
```

`caffeinate -d -t 7200` — Keep display on for a 2-hour presentation.

```bash
caffeinate -d -t 7200
```

`caffeinate -i make -j$(sysctl -n hw.ncpu)` — Keep system awake during a long compilation.

```bash
caffeinate -i make -j$(sysctl -n hw.ncpu)
```

`caffeinate -is npm run build` — Keep system awake during a long build process.

```bash
caffeinate -is npm run build
```

<!-- PROSE:outro -->
## Conclusion

caffeinate is a classic Unix tool: one clearly defined job, done with a handful of flags. The most elegant way to use it is to put the actual command right after it – then the Mac stays awake exactly as long as the task takes and automatically returns to its normal power-saving behaviour afterwards. There is nothing to change in System Settings and nothing to undo later. For permanent changes to sleep behaviour, `pmset` remains the better tool; caffeinate is the quick, temporary solution for the moment.

## Further Reading

- [Change Energy Saver settings on Mac](https://support.apple.com/guide/mac-help/mchle41a6ccd/mac) – Apple Support on sleep and power options
- [caffeinate(8) – manual pages](https://ss64.com/mac/caffeinate.html) – detailed reference for every flag
<!-- PROSE:outro:end -->

## Related Commands

- [defaults](https://www.jpkc.com/db/en/cheatsheets/macos/defaults/) – read and write macOS user preferences
- [diskutil](https://www.jpkc.com/db/en/cheatsheets/macos/diskutil/) – manage disks, volumes and partitions
- [dscl](https://www.jpkc.com/db/en/cheatsheets/macos/dscl/) – command-line access to macOS directory services

