18

Is there a way to prevent Windows from going into sleep mode while running a program on the command line or with cygwin? Ideally, I'm hoping for something that could be used like:

nosleep myscript.sh

Background

Sometimes I start up long-running jobs like a manual backup or large file transfers and I've found that Windows often goes to sleep before these finish. I'd like to be able to start up the command and prevent sleep mode while the command is running, but have it automatically work again once the command has completed.

5 Answers 5

18

You may use powercfg in a script to change the time the PC waits until it goes to sleep:

Never go to standby:

powercfg -change -standby-timeout-ac 0

Go to standby in 15 minutes:

powercfg -change -standby-timeout-ac 15
2
  • Thanks. powercfg plus some bash magic are just what I needed.
    – Mr Fooz
    Jan 2, 2010 at 16:39
  • Unfortunately this wipes out the previous setting, and you have to restore it manually.
    – Leeroy
    Jul 15, 2019 at 20:14
8

Here's a bash script that I whipped up based on harrymc's response.

#!/usr/bin/bash

# NAME
#   nosleep - prevent sleep and hibernation while running a command
#
# SYNOPSIS
#   nosleep COMMAND [ARG]...

# Make sure the power scheme gets restored, even if Ctrl-C happens
cleanup()
{
  powercfg -setactive $SCHEME_GUID
  powercfg -delete    $TMP_GUID
  return $?
}
trap cleanup SIGINT

# Disable sleep and hibernate timers
export SCHEME_GUID=`powercfg -getactivescheme | gawk '{ print $4 }'`
export TMP_GUID=`powercfg -duplicatescheme $SCHEME_GUID | gawk '{ print $4 }'`
if [[ -z $TMP_GUID ]]; then
    echo "ERROR: could not duplicate the current power scheme"
    exit 254
fi
powercfg -setactive $TMP_GUID
powercfg -changename $TMP_GUID nosleep "temporary scheme for disabling sleep and hibernation"
powercfg -change -standby-timeout-ac 0
powercfg -change -hibernate-timeout-ac 0

# Run the command
"$@"

powercfg -setactive $SCHEME_GUID
powercfg -delete    $TMP_GUID
3
  • On Cygwin, I had to remove the quotes: From "$*" to $*. Otherwise I got the following when giving more than one argument (e.g. $nosleep sleep 1: /usr/bin/nosleep: line 31: sleep 1 command not found
    – Nippey
    Aug 2, 2016 at 6:48
  • 1
    @Nippey, that should actually be "$@". "$*" is wrong because it concatenates all the arguments, while $* is wrong as it splits the arguments on $IFS and removes the empty ones.
    – sch
    Dec 13, 2016 at 13:28
  • This neatly makes sure the power scheme gets restored, even if Ctrl-C happens, but what would be even neater would be to just create a power request keeping the computer awake for the duration of the script, without permanently changing any settings. But it looks like there isn't any clean, easy way to do that so managing state it is. However, a hidden media player playing silence would be a dirty but easy way. In AutoHotkey I could even get away with using a default Windows sound (yay, portable script). Loop { SoundPlay, %A_WinDir%\Media\ding.wav }, without wait argument.
    – Leeroy
    Jul 15, 2019 at 20:00
6

There is now such a nosleep command in Cygwin. Just install the nosleep package and run as

nosleep myscript.sh

Written by Andrew E. Schulman in 2011. See https://cygwin.com/ml/cygwin/2011-09/msg00151.html

The source on Launchpad. It uses SetThreadExecutionState() (like Insomnia already mentioned), doesn't create a separate power scheme.

Usage: nosleep [OPTION...] command [args]
Run a command while inhibiting computer sleep or hibernation.

  -a, --awaymode             Force away mode instead of sleep mode
  -d, --display              Keep the display on
  -i, --ifacpower            Following options only apply if AC power is on
  -?, --help                 give this help list
  --usage                give a short usage message
  -V, --version              print program version

Report bugs to the Cygwin mailing list <[email protected]>.

Note that it prevents the system from automatically going to sleep on idle, not the system from going to sleep if requested by a user (like when closing a laptop lid).

3

Suggestion from this answer by @LorenzCK on a related question:

Use Presenting Mode from Windows Mobility Center (if available)

%WINDIR%\System32\PresentationSettings.exe "/start"

Check with powercfg -requests in the command prompt. Should output:

DISPLAY:
[PROCESS] \Device\HarddiskVolume2\Windows\System32\PresentationSettings.exe
SYSTEM:
[PROCESS] \Device\HarddiskVolume2\Windows\System32\PresentationSettings.exe

Upside is it gets cleared on shutdown or restart. Now you don't have to worry about storing and restoring the original power settings 👍

It also prevents monitors going to sleep but you can make that happen with a command in your script. Simple but third-party solutions: NirCmd nircmd monitor off or AutoHotkey SendMessage, 0x112, 0xF170, 2,, Program Manager The built-in powercfg /requestsoverride process PresentationSettings.exe display will not work though.

2

Insomnia prevent your windows to go to sleep but it's not a command line tools, so your script with the harrymc commands are better solutions

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .