Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to teach myself Python. As a small intro project, I've trying to build a program which allows you to turn off or send a computer to sleep after a user defined set of time.

I had no issue with turning the computer off, but am having difficulty sending it to sleep (after 60 minutes for example)

I have the command to send the computer to sleep but I am having difficulty setting the time in which the program waits before it happens:

powercfg -h off & start /min "" C:\WINDOWS\System32\rundll32.exe PowrProf.dll,SetSuspendState 0,1,0 & ping -n 3 > nul & powercfg -h on & exit

Thank you for taking the time to read this, if you could point me in the right direction I would be extremely thankful.

share|improve this question
The make shift timer is set to 3 seconds as -n 3 will take 3 seconds to perform set it to 3600 and test – 50-3 Sep 16 '13 at 22:20
I would suggest changing the Vbs or powershell if you want easy to control delays – 50-3 Sep 16 '13 at 22:21
More of a programming question and better suited for SO than here . . . – ernie Sep 16 '13 at 22:47

Your question has little to do with python, as the command you've given in just command line on Windows, including the makeshift timer. Looking over the code you've shown, it's just a bunch of command line commands strung together with & . . .

powercfg -h off & start /min "" C:\WINDOWS\System32\rundll32.exe PowrProf.dll,SetSuspendState 0,1,0 & ping -n 3 > nul & powercfg -h on & exit

Breaking this down, the command does:

  1. powercfg -h off : turns off hibernation
  2. start /min "" C:\WINDOWS\System32\rundll32.exe PowrProf.dll,SetSuspendState 0,1,0 - I'm not 100% sure on this : puts the computer to sleep
  3. ping -n 3 > nul : this is a common timer hack for Windows, where it basically introduces a 3 second delay; I'm going to guess this is to handle some timing issues when a system is waking up from sleep, as this command won't run till you're re-waking
  4. powercfg -h on : re-enables hibernation
  5. exit : exits the batch file.

So, there's no delay here . . . to add a delay in python, you can use the time module's sleep function. Some example code would be something like:

import time
# Wait 10 seconds before running the next command
delay = 10
print "hi . . . it's been {0} seconds!".format(delay)
share|improve this answer

Your Answer


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.