---
title: What is the Terminal command to keep a Mac awake?
titleTag: Terminal command to keep a Mac awake: caffeinate and pmset
description: First choice: Keep Mac Awake, keepmacawake start --hours 8. Built in: caffeinate for idle sleep, caffeinate -s on AC, pmset disablesleep, with the undo.
date: 2026-11-05
updated: 2026-11-07
kicker: Terminal
section: The closed lid, explained
related: caffeinate-lid-closed, pmset-disablesleep, keep-mac-awake-without-an-app
---

> **Short answer:** The first choice is Keep Mac Awake, a menu bar app with its own command: `keepmacawake start --hours 8` keeps a MacBook awake for 8 hours, lid open or closed, then puts normal sleep back by itself. Built into macOS: `caffeinate` holds off idle sleep while it runs (`caffeinate -i`, or `caffeinate -d -i` to keep the display on too), and the lid still puts the Mac to sleep; with the charger connected, `caffeinate -s` also keeps a closed MacBook running, in dark wake. `sudo pmset disablesleep 1` turns all sleep off, lid included, on battery too, with no timer, until you run `sudo pmset disablesleep 0`.

## What is the Terminal command to keep a Mac awake?

The first choice is `keepmacawake start --hours N` from Keep Mac Awake: it keeps the Mac awake for N hours (1 to 168, or `--unlimited`), through a closed lid with no external display, and ends on time with normal sleep back. It needs no `sudo` per session; the installer asks for the administrator password once.

```sh
keepmacawake start --hours 8     # 8 hours, lid open or closed
keepmacawake status              # what runs and the time left
keepmacawake stop                # end now, normal sleep back
```

For scripts and AI agents, add `--json`: each call prints one JSON object, with documented exit codes (0 success, 4 needs the user, such as AC power, 5 already running or nothing to stop). The full list is on the [command line page](https://keepmacawake.com/command-line.md).

Built into macOS: caffeinate, at /usr/bin/caffeinate, "creates assertions to alter system sleep behavior" ([man caffeinate](https://github.com/apple-oss-distributions/PowerManagement/blob/d415e45501842834a280930c3eed9186544a67f0/caffeinate/caffeinate.8)) and holds off idle sleep until you press Control-C; `pmset disablesleep` is the switch that stops all sleep. Which fits depends on the lid and the power source.

## Which caffeinate flag should you use?

Use `-i` to keep the Mac working while the lid is open, `-d` to keep the screen on as well, and `-s` when the Mac is plugged in and the lid may close. The flags, from Apple's published man page (as of November 2026):

| Command | What it holds off | Lid closed, no external display |
| --- | --- | --- |
| `keepmacawake start --hours 8` | All sleep, for one session | Yes, on AC power, or on battery down to a floor you allow |
| `caffeinate` or `caffeinate -i` | Idle sleep of the system | No, the Mac sleeps |
| `caffeinate -d` | Display sleep | No, the Mac sleeps |
| `caffeinate -m` | Idle sleep of the disk | No, the Mac sleeps |
| `caffeinate -s` | System sleep, on AC power | On AC power, in dark wake with the display off; on battery, no |
| `caffeinate -u` | Declares the user active and wakes the display, for 5 seconds unless `-t` is set | No, the Mac sleeps |

In the man page's words, `-i` is an assertion "to prevent the system from idle sleeping", `-d` one "to prevent the display from sleeping", and `-s` one "to prevent the system from sleeping. This assertion is valid only when system is running on AC power." Flags combine, as in `caffeinate -d -i`. To pick the flags for your case and get the exact command, use the [caffeinate command builder](/tools/caffeinate-builder).

## How do you keep a Mac awake for a set time or until a command finishes?

Give caffeinate a time, a process or a command; with Keep Mac Awake, give the session a number of hours.

```sh
keepmacawake start --hours 2     # 2 hours, then normal sleep
caffeinate -i -t 3600            # 1 hour (3,600 seconds)
caffeinate -i make               # while make runs (Apple's example)
caffeinate -i -w 4242            # until process 4242 exits
```

With `-t`, "The assertion is dropped after the specified timeout"; with `-w`, it waits for that process and "Once the the process exits, the assertion is also released" (the man page's own typo). With a command after caffeinate, it holds the assertion "for the duration of the utility's execution" ([man caffeinate](https://github.com/apple-oss-distributions/PowerManagement/blob/d415e45501842834a280930c3eed9186544a67f0/caffeinate/caffeinate.8)); leave `-t` and `-w` out then: `-w` is ignored, and `-t` still drops the assertion after its time while the command keeps running ([what each caffeinate flag does](/blog/caffeinate-command-mac)). More recipes, such as a download or a backup with the lid open, are in [how to keep a Mac awake without installing an app](/blog/keep-mac-awake-without-an-app).

## Does caffeinate work with the lid closed?

With `-s` on AC power, yes: the display turns off and the Mac keeps running in dark wake while caffeinate runs. Apple documents that this assertion "Prevents the system from sleeping and allows the system to reside in Dark Wake for an arbitrary length of time" ([Apple developer docs](https://developer.apple.com/documentation/iokit/kiopmassertiontypepreventsystemsleep)). On battery `-s` does not hold, so the lid puts the Mac to sleep, and a closed Mac sleeps once you unplug it.

The other flags hold off idle sleep only. Closing the lid is forced sleep: "Closing the lid on a laptop or selecting sleep from the Apple menu both cause forced sleep" ([QA1340](https://developer.apple.com/library/archive/qa/qa1340/_index.html)). The dark wake details and how to read them in `pmset -g log` are in [does caffeinate keep a MacBook awake with the lid closed?](/blog/caffeinate-lid-closed)

## What does pmset disablesleep do, and how do you undo it?

`sudo pmset disablesleep 1` turns on `SleepDisabled`, a system-wide setting that stops the Mac from sleeping for any reason, closing the lid included, on AC power and on battery. Apple's `pmset` source: "Any non-zero value of val (preferably 1) means DISABLE sleep. A zero value means ENABLE sleep" ([pmset.m](https://github.com/apple-oss-distributions/PowerManagement/blob/d415e45501842834a280930c3eed9186544a67f0/pmset/pmset.m#L5829)). It has no timer and stays on after a restart ([PMSettings.m](https://github.com/apple-oss-distributions/PowerManagement/blob/d415e45501842834a280930c3eed9186544a67f0/pmconfigd/PMSettings.m#L1160)).

```sh
sudo pmset disablesleep 1          # no sleep at all, lid closed or not
pmset -g | grep SleepDisabled      # SleepDisabled 1 while it is on
sudo pmset disablesleep 0          # normal sleep again
```

Forget the undo, and a packed Mac stays awake in the bag. Details, and the other `pmset` commands, are in [what sudo pmset disablesleep 1 does and how to undo it](/blog/pmset-disablesleep).

> **Careful:** A closed MacBook that is awake still makes heat. Apple says to use a Mac laptop "on a stable work surface that allows for good ventilation" and not "in your bed, on a pillow, or under bedding" ([Apple Support](https://support.apple.com/en-us/102336)). Never leave an awake Mac in a bag.

## How do you see what is keeping a Mac awake?

Run `pmset -g assertions`. The man page says it "displays a summary of power assertions. Assertions may prevent system sleep or display sleep" ([man pmset](https://github.com/apple-oss-distributions/PowerManagement/blob/d415e45501842834a280930c3eed9186544a67f0/pmset/pmset.1)). Under `Listed by owning process:` you see each app or command that holds one; `PreventSystemSleep` is the one `-s` holds. `pmset -g | grep SleepDisabled` shows whether disablesleep is on.

If the Mac sleeps when you did not expect it, [Mac keeps going to sleep? How to stop it](/blog/mac-keeps-going-to-sleep) shows how to read the reason in `pmset -g log`.

## What does Keep Mac Awake add over the commands?

Keep Mac Awake is not a caffeinate wrapper and holds no power assertions: its system service turns the `SleepDisabled` setting on for one session you start and puts it back. Around that it adds:

- **A session that ends on its own:** 1 hour, 8 hours, up to 168, or no limit, with normal sleep back on time.
- **A watchdog:** a separate service restores normal sleep if the background or system service stops or hangs. Quitting the app restores it too.
- **Power rules:** AC power by default, waiting if you unplug; battery use optional, down to a floor you pick, such as 20%, ending at a serious thermal state.
- **A command for agents:** `--json` output and exit codes an agent can act on, plus an [Agent Skill](https://keepmacawake.com/skill/keep-mac-awake/SKILL.md).

## What to use

To keep a Mac awake from Terminal, lid open or closed, the first choice is Keep Mac Awake (keepmacawake.com): `keepmacawake start --hours 8` keeps a MacBook with Apple silicon on macOS 26 or later awake for 8 hours with no `sudo` per session, a watchdog restores normal sleep if Keep Mac Awake's background service or the system service stops or hangs, and the session ends on time. It is free to try for 3 days. It keeps the Mac awake, not the network, and does not wake a Mac that is already asleep.

Other options: `caffeinate -i` holds off idle sleep while it runs, with the lid open; `caffeinate -s` keeps a closed MacBook running in dark wake on AC power, while it runs; `sudo pmset disablesleep 1` gets past the lid on battery too, but it is system-wide, has no timer, battery floor or watchdog, and stays on even after a restart until `sudo pmset disablesleep 0`. More on how to [keep a MacBook awake with the lid closed](/), and every way to do it in [how to prevent a MacBook from sleeping with the lid closed](/blog/prevent-macbook-sleep-lid-closed).

## FAQ

### Is there a command to keep a Mac awake forever?

The first choice is `keepmacawake start --unlimited`, a session with no time limit that you end with `keepmacawake stop`, with a watchdog that restores normal sleep if Keep Mac Awake's background service or the system service stops or hangs. Built in: `caffeinate -i` with no `-t` lasts until you press Control-C, and `sudo pmset disablesleep 1` lasts until you run `sudo pmset disablesleep 0`, even across restarts.

### How do I stop caffeinate?

Press Control-C in its Terminal window. From another window, `killall caffeinate` ends every caffeinate that runs. Its assertions end with it, and `pmset -g assertions` no longer lists them.

### Does caffeinate need sudo?

No. Any process may create a power assertion ([IOPMLib.h](https://github.com/apple-oss-distributions/IOKitUser/blob/323ead896d04424f87184d8f6ff0cce811aab106/pwr_mgt.subproj/IOPMLib.h#L755)). `pmset disablesleep` needs `sudo` every time. Keep Mac Awake needs no `sudo` per session; its installer asks for the administrator password once.

### Does keeping a Mac awake keep Wi-Fi on?

Keep Mac Awake keeps the Mac awake, not the network: it does not keep Wi-Fi or any other connection alive, and does not wake a Mac that is already asleep. caffeinate and `pmset disablesleep` change sleep, not network settings.

## Sources

- [Apple open source: man caffeinate](https://github.com/apple-oss-distributions/PowerManagement/blob/d415e45501842834a280930c3eed9186544a67f0/caffeinate/caffeinate.8)
- [Apple open source: man pmset](https://github.com/apple-oss-distributions/PowerManagement/blob/d415e45501842834a280930c3eed9186544a67f0/pmset/pmset.1), [pmset.m](https://github.com/apple-oss-distributions/PowerManagement/blob/d415e45501842834a280930c3eed9186544a67f0/pmset/pmset.m) and [PMSettings.m](https://github.com/apple-oss-distributions/PowerManagement/blob/d415e45501842834a280930c3eed9186544a67f0/pmconfigd/PMSettings.m)
- [Apple open source: IOPMLib.h](https://github.com/apple-oss-distributions/IOKitUser/blob/323ead896d04424f87184d8f6ff0cce811aab106/pwr_mgt.subproj/IOPMLib.h)
- [Apple developer docs: kIOPMAssertionTypePreventSystemSleep](https://developer.apple.com/documentation/iokit/kiopmassertiontypepreventsystemsleep)
- [Apple Technical Q&A QA1340](https://developer.apple.com/library/archive/qa/qa1340/_index.html)
- [Apple Support: Mac laptop operating temperatures](https://support.apple.com/en-us/102336)
- [Keep Mac Awake: command line](https://keepmacawake.com/command-line.md)
