Catfood Software Blog

 

Prevent Sleep During Scheduled Tasks

I've been trying to cut back on the amount of power we use at home. This is partly virtuous, but mainly because I've started tracking power consumption vs. generation for our solar panels (even though it's December I want to try and have a day of net generation, you can see my progress on this chart).

I bought a couple of smart strips and now have both main computers set to switch all their peripherals off when they go to sleep. This goes from 250-300 watts to 1-2 watts - very cool.

There was a problem though. I run all sorts of overnight processing - backing up databases, processing web stats, updating the webcam list for WebCamSaver - and the computer kept going to sleep in the middle of it.

Task scheduler lets you wake your computer to run a task, but there doesn't seem to be an option to stop it sleeping until the task has finished. I hunted around for a while, but it seemed like one of those times when it's just faster to write something.

Nosleep (source) is a command line tool that disables sleep or hibernate, runs a batch file or program and then enables sleep or hibernate again. It will run on any recent version of Windows - I use it from task scheduler to launch my monster batch file of overnight tasks.

It's a simple little app - it just calls SetThreadExecutionState() before and after launching the target batch file. The trick with SetThreadExecutionState is that the new state doesn't take the first time you call the function. Calling it twice has the desired effect.

Labels:

Posted by Rob on 12/07/2008, 6 comments. (Bookmark & Share)

 

Comments

Subscribe to comments on this post.

 

Thanks for the no sleep script. Very helpful and appreciated!

By Anonymous Anonymous, at March 23, 2009 7:10 AM  

 

Rob,
Thanks for this utility and making the source code available, too. I think it is precisely what I've been looking for.

By Blogger Kent, at April 5, 2009 8:01 AM  

 

wait - what do you mean you run it before and after the schedule task or that you need to call it twice?

Also, how do you reenable sleep mode after the tasks finish?

TIA.

By Anonymous Anonymous, at July 17, 2009 10:28 PM  

 

TIA: it's a wrapper, nosleep runs the task and then stays resident until the task completes.

The way I use it is to put the task (or tasks) in a batch file and then call the batch file from nosleep:

nosleep.exe mytasks.bat

The batch file is the parameter to nosleep. Hope this helps, if not email me.

By Blogger Rob, at July 18, 2009 9:34 AM  

 

Awesome thanks so much for this. This problem has been with me ever since i moved to vista. Trying out your nosleep solution now.

One question, does it support program arguments?
e.g.
nosleep myprog /a /b /c

or do i need to set them inside a batch file?

By Anonymous Royce, at December 16, 2009 3:04 PM  

 

Royce, great, let me know how you get on.

Parameters are not passed so you'll need to use a batch file as you suggest.

By Blogger Rob, at December 16, 2009 6:57 PM  

 

Post a Comment

 

Links

Create a Link