Skip to content

Instantly share code, notes, and snippets.

@AzzyC
Last active February 20, 2021 13:22
Show Gist options
  • Save AzzyC/09f27bc06bd9010bf72ef1413adb9986 to your computer and use it in GitHub Desktop.
Save AzzyC/09f27bc06bd9010bf72ef1413adb9986 to your computer and use it in GitHub Desktop.
Completely disable sleep on Windows
#!/bin/bash
# Restore original and remove new power scheme, if User signals interruption
# Restore trap necessary, otherwise script will not work in subsequent instances
restore () {
powercfg -setactive "$activescheme" 2> /dev/null
powercfg -delete "$newscheme" 2> /dev/null
}
trap restore SIGINT
# Get User's current power scheme to restore and use its duplicate to tweak
activescheme="$(powercfg -getactivescheme | awk '{print $4}')"
newscheme="$(powercfg -duplicatescheme "$activescheme" | awk '{print $4}')"
# Distinctly name tweaked power scheme and set it to active
powercfg -changename "$newscheme" "No Sleep"
powercfg -setactive "$newscheme"
# Note: AC means while charging; DC means off battery. Tweak to desire; Value = minutes. Currently all sleep params disabled
powercfg -change monitor-timeout-ac 0
powercfg -change monitor-timeout-dc 0
powercfg -change disk-timeout-ac 0
powercfg -change disk-timeout-dc 0
powercfg -change standby-timeout-ac 0
powercfg -change standby-timeout-dc 0
powercfg -change hibernate-timeout-ac 0
powercfg -change hibernate-timeout-dc 0
# Run any function/script alongside executing this script e.g: $ ./nosleep.sh ./screenrecord.sh
# or even $ ./nosleep.sh sleep 1h while leaving a task running in another window
"$@"
# Restore original power scheme, delete tweaked one
restore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment