It is a common problem that long-running programs are interrupted by the system going to sleep. Examples that benefit from caffeinate.py across operating systems include:
- "rsync" or "scp" remote file transfers
- long-running computations / simulations
- "ffmpeg" video processing
- CMake / Ninja / Make builds of large projects
This Python utility caffeinate.py runs a program while preventing the system from going to sleep. It works on Windows, macOS and Linux.
One can find several prior Python attempts going back over 10 years, but they typically aren't for calling a specific program and then automatically reallowing sleep when the program finishes, even if the called program crashes or errors. They also tend to be platform-specific.
Verify operation of caffeinate.py using SetThreadExecutionState on Windows by opening a second terminal as Administrator and check:
powercfg /requests
before, during and after running a command with caffeinate.py
.
Note: WSL (Windows Subsystem for Linux) might not be able to prevent sleep on Windows, even when systemd-inhibit is available in the
WSL environment.
If this is an issue, run the command directly in Windows Command Prompt or PowerShell by invoking python3 caffeinate.py wsl.exe <command> <args>
from the Windows Terminal.
caffeinate.py
uses the macOS factory built-in
caffeinate
command.
Verify operation of caffeinate.py on macOS by opening a second terminal and check
pmset -g assertions
On Linux, caffeinate.py
uses the built-in
systemd-inhibit command if available.
Verify operation of caffeinate.py on Linux by opening a second terminal and check:
systemd-inhibit --list