o
odin.langpkg.dev
packages / tool / stay-awake-odin

stay-awake-odin

v0.2.0tool

Keep a Windows machine awake

Apache-2.0 ยท updated 2 years ago

stay-awake-odin

stay-awake-odin

Keep a Windows machine awake

Get

Executable binaries for Windows may be found at the Releases page.

Usage

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

System

The simplest use case is to run the executable without any switches.

stay-awake.exe

This 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.

Display

To keep the machine awake and prevent the display from turning off, utilize the -display switch.

stay-awake.exe -display

This 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 SetThreadExecutionState function (which is the Win32 function called by stay-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.

Help

Result of running stay-awake.exe -help

Usage:
        stay-awake.exe [-display]
Flags:
        -display  | Keep display on

Testing

Testing - either automated or manual - utilizes the powercfg command line tool to validate the execution state set via SetThreadExecutionState.

Automated

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.bat

The 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.

Manual

Run the executable stay-awake.exe. Open PowerShell (or a command prompt) with elevated (admin) privileges. Execute the following.

powercfg -requests

The execution states will be listed along with any programs that have made calls to SetThreadExecutionState. Manually validate execution states are set as expected.

Compile

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.

GitHub Actions

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 --tags

Win32 Docs

The application utilizes SetThreadExecutionState from the Win32 API.

Prior Implementations

Alternate Tools