Feb-05, 2025 · 3min
change language学习如何使用 Mac 的 caffeinate 命令防止系统睡眠,确保长时间运行的程序(如开发服务器)不会因系统休眠而中断
有时候一些程序需要长时间运行,比如 npm run dev
或者 pnpm dev
,这些程序运行时,当 Mac 进入睡眠状态,可能会导致程序中断。
可以使用 caffeinate
命令来防止 Mac 进入睡眠状态。
caffeinate -i npm run dev
❯ caffeinate --help
caffeinate: illegal option -- -
usage: caffeinate [-disu] [-t timeout] [-w Process ID] [command arguments...]
# 阻止机器休眠(后台跑程序、听歌等,屏幕过段时间后会自动关闭)
caffeinate
# 阻止屏幕(-d display)休眠
caffeinate -d
# 阻止休眠一段时间(-t [阻止休眠秒数])
caffeinate -t 3600
# 该命令能让进程一直保持运行
caffeinate -w <进程号>