Feb-05, 2025 · 3min
change languageLearn how to use the caffeinate command on Mac to prevent system sleep, ensuring that long-running programs (such as development servers) will not be interrupted by system sleep
Sometimes some programs need to run for a long time, such as npm run dev
or pnpm dev
, these programs run when Mac enters sleep, which may cause the program to interrupt.
You can use the caffeinate
command to prevent Mac from entering sleep.
caffeinate -i npm run dev
❯ caffeinate --help
caffeinate: illegal option -- -
usage: caffeinate [-disu] [-t timeout] [-w Process ID] [command arguments...]
# Prevent machine sleep (run programs in the background, listen to music, etc., the screen will automatically close after a period of time)
caffeinate
# Prevent screen sleep (-d display)
caffeinate -d
# Prevent sleep for a period of time (-t [prevent sleep seconds])
caffeinate -t 3600
# This command keeps the process running
caffeinate -w <进程号>