Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 8 down vote accepted

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

Never go to standby:

powercfg -change -standby-timeout-ac 0

Go to standby in 15 minutes:

powercfg -change -standby-timeout-ac 15

share|improve this answer
    
Thanks. powercfg plus some bash magic are just what I needed. –  Mr Fooz Jan 2 '10 at 16:39

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
share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.