Mac caffeinate 命令
caffeinate 是 macOS 系统中的一个强大的命令行工具,它诞生于 Darwin 操作系统环境,专为解决一个常见痛点而设计:阻止系统在执行重要任务时进入睡眠状态。正如其名(caffeinate 意为“摄入咖啡因”),它能让您的 Mac “保持清醒”,确保耗时或后台任务能够不间断地完成。
一、核心功能与机制
1. 核心用途:创建系统断言 (Assertions)
caffeinate 的工作原理是通过创建“断言”(assertions)来改变系统的电源管理行为。系统电源管理模块会检查当前是否有活动的断言。只要断言存在,系统就会遵循断言的要求,不执行相应的睡眠操作。
2. 基本语法结构
caffeinate [选项] [utility arguments...]• [选项]: 用于指定要阻止哪种类型的睡眠(如屏幕、磁盘、闲置等)。
• [utility arguments...]: 可选,指定要运行的程序及其参数。
3. 两种主要工作模式
| 模式 | 描述退出条件 | 退出条件 |
| 附着模式 (Utility Mode) | 如果您在命令后指定了一个程序(例如 caffeinate make),caffeinate 会为该程序创建断言。 | 程序执行完毕,caffeinate 随之退出,断言自动释放。 |
| 独立模式 (Assertion Mode) | 如果未指定程序(仅使用 caffeinate 和选项),caffeinate 进程本身就持有断言。 | 用户手动按下 Ctrl + C 终止 caffeinate 进程,断言释放。 |
二、详细选项(断言标志)解析
caffeinate 提供了精细的控制,您可以根据任务的需要选择性地阻止不同类型的睡眠。
| 选项 | 断言类型 | 详细描述 | 场景举例 |
| 无选项 或 -i | 阻止闲置睡眠 (Prevent idle sleep) | 阻止系统在达到用户设置的闲置时间后自动进入睡眠。这是最常用的默认行为。 | 后台运行一个需要持续连接网络的脚本。 |
| -d | 阻止显示器睡眠 (Prevent display sleep) | 阻止显示器变暗或关闭。系统本身可能仍能进入闲置睡眠(如果未指定 -i)。 | 观看长时间视频或演示文稿。 |
| -m | 阻止磁盘睡眠 (Prevent disk idle sleep) | 阻止系统在磁盘闲置一段时间后将其关闭以节省电力。 | 运行需要频繁小规模读写磁盘的数据库操作。 |
| -s | 阻止系统睡眠 (Prevent system sleep) | 创建最高级别的断言,阻止整个系统睡眠。重要:此断言仅在设备使用交流电 (AC Power) 时有效。 | 运行必须不中断的系统升级或大型备份。 |
| -u | 用户活动声明 (User is active) | 声明用户处于活动状态。如果显示器关闭,它会开启显示器并阻止显示器闲置。默认有 5 秒的超时。 | 短暂地保持屏幕亮起,模仿用户活动。 |
三、时间和进程控制选项
除了断言类型,您还可以控制断言的持续时间或与特定进程的绑定。
| 选项 | 描述 | 注意事项 |
| -t timeout | 指定断言的有效时间(单位:秒)。时间到达后,断言自动释放。 | 仅在独立模式下使用,当附着于程序时,此选项被忽略。 |
| -w pid | 等待具有指定 PID 的进程退出。该进程退出后,断言立即释放。 | 仅在独立模式下使用,当附着于程序时,此选项被忽略。 |
四、常用示例与场景应用
1. 编译或安装软件时保持系统清醒
在编译大型项目(如使用 make)或安装软件时,如果系统休眠,任务可能会中断。
caffeinate -i make解释: 在 make 命令执行期间,阻止系统进入闲置睡眠。
2. 长时间阻止屏幕和系统休眠
在观看电影或进行持续的屏幕演示,且需要确保系统不会因电源管理而干扰时。
caffeinate -d -s解释: -d 阻止显示器睡眠,-s 阻止系统睡眠(确保连接电源)。需要手动按 Ctrl + C 退出。
3. 设置限时防休眠
只在接下来的 3600 秒(1小时)内阻止系统闲置睡眠。
caffeinate -t 3600解释: 保持闲置睡眠断言 1 小时,时间一到,断言自动解除。
4. 绑定到后台进程
如果有一个已经在后台运行的进程,您可以让 caffeinate 绑定它,并在它结束时自动退出。
# 假设后台运行的进程 PID 是 12345
caffeinate -w 12345解释: 只要 PID 为 12345 的进程仍在运行,caffeinate 就会保持默认的闲置睡眠断言。
总结
caffeinate 是 macOS 用户和开发者工具箱中一个高效且优雅的工具。通过创建精细化的系统断言,它允许用户在不更改全局电源设置的情况下,确保关键任务的顺畅执行,是提高工作流可靠性的重要命令行伙伴。
🤖 文章部分内容由 Google Gemini 生成
caffeinate 命令详细
CAFFEINATE(8) System Manager's Manual CAFFEINATE(8)
NAME
caffeinate – prevent the system from sleeping on behalf of a utility
SYNOPSIS
caffeinate [-disu] [-t timeout] [-w pid] [utility arguments...]
DESCRIPTION
caffeinate creates assertions to alter system sleep behavior. If no
assertion flags are specified, caffeinate creates an assertion to prevent
idle sleep. If a utility is specified, caffeinate creates the assertions
on the utility's behalf, and those assertions will persist for the duration
of the utility's execution. Otherwise, caffeinate creates the assertions
directly, and those assertions will persist until caffeinate exits.
Available options:
-d Create an assertion to prevent the display from sleeping.
-i Create an assertion to prevent the system from idle sleeping.
-m Create an assertion to prevent the disk from idle sleeping.
-s Create an assertion to prevent the system from sleeping. This
assertion is valid only when system is running on AC power.
-u Create an assertion to declare that user is active. If the display
is off, this option turns the display on and prevents the display
from going into idle sleep. If a timeout is not specified with '-t'
option, then this assertion is taken with a default of 5 second
timeout.
-t Specifies the timeout value in seconds for which this assertion has
to be valid. The assertion is dropped after the specified timeout.
Timeout value is not used when an utility is invoked with this
command.
-w Waits for the process with the specified pid to exit. Once the the
process exits, the assertion is also released. This option is
ignored when used with utility option.
EXAMPLE
caffeinate -i make
caffeinate forks a process, execs "make" in it, and holds an assertion
that prevents idle sleep as long as that process is running.
SEE ALSO
pmset(1)
LOCATION
/usr/bin/caffeinate
Darwin November 9, 2012 Darwin