Keep a Windows machine awake
Executable binaries for Windows may be found at the Releases page.
The executable stay-awake.exe is intended to be run in a terminal in order to keep one's Windows machine awake.
There are two modes one may choose from:
- System [Default] โ the machine will not go to sleep but the display could turn off
- Display โ the machine will not go to sleep and the display will remain on
The simplest use case is to run the executable without any switches.
stay-awake.exeThis will prevent the machine from going to sleep and will await the user pressing the Enter key within the terminal before resetting the machine state.
To keep the machine awake and prevent the display from turning off, utilize the -display switch.
stay-awake.exe -displayThis will prevent the machine from going to sleep (while also keeping the display on) and will await the user pressing the Enter key within the terminal before resetting the machine state.
๐ As noted in the Win32 documentation, use of the
SetThreadExecutionStatefunction (which is the Win32 function called bystay-awake.exe) does not prevent one from putting their computer to sleep by either closing the lid on their laptop or pressing the power button. In addition, the screen saver may still execute.
Result of running stay-awake.exe -help
Usage:
stay-awake.exe [-display]
Flags:
-display | Keep display on
Testing - either automated or manual - utilizes the powercfg command line tool to validate the execution state set via SetThreadExecutionState.
Run the test.bat file which utilizes the Odin test runner. This assumes that sudo is setup and available for use from the command line.
.\test.batThe test.bat file runs tests with 1 thread. If tests were run in parallel (the default) then the thread execution state could be overwritten by a different thread and tests would no longer be valid.
Finally, note that there are 5 tests and each test requires elevated (admin) privileges; thus the user will be prompted to run sudo 5 times.
Run the executable stay-awake.exe. Open PowerShell (or a command prompt) with elevated (admin) privileges. Execute the following.
powercfg -requestsThe execution states will be listed along with any programs that have made calls to SetThreadExecutionState. Manually validate execution states are set as expected.
First, ensure Odin has been installed and is available on one's PATH. Clone this repository and then run the build.bat file.
git clone https://github.com/curtisalexander/stay-awake-odin.git
cd stay-awake-odin
.\build.bat
The resulting executable will be stay-awake.exe.
Below is the rough git tag dance to delete and/or add tags to trigger GitHub Actions.
# delete local tag
git tag --delete v0.1.0
# delete remote tag
git push origin --delete v0.1.0
# add and commit local changes
git add .
git commit -m "commit msg"
# push local changes to remote
git push
# add local tag
git tag -a v0.1.0 -m "v0.1.0"
# push local tag to remote
git push origin --tagsThe application utilizes SetThreadExecutionState from the Win32 API.
C#PowerShellRust v1- Loads
kernel32.dlland performs a transmute to get the function SetThreadExecutionState
- Loads
Rust v2- Makes use of the windows crate rather than transmute as is done in stay-awake-rs
- Microsoft PowerToys includes the Awake utility
- It also utilizes SetThreadExectionState to keep a Windows machine awake