nosleep command

Andrew Schulman schulman.andrew@epamail.epa.gov
Mon Sep 12 17:42:00 GMT 2011


I'd like to provide a 'nosleep' command for Cygwin, that will prevent the host from sleeping or
hibernating while a command executes.  I haven't seen anything in Cygwin that currently does this,
but please enlighten me if I'm wrong.

Looking around for how to do this, I see two basic approaches:

(1) Run SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED) to try to convince the computer
not to sleep or hibernate, while the desired command executes.  See for example [1], which does
this.  I haven't tested it yet.  Looking at the documentation of SetThreadExecutionState() [2], it
seems that this approach will keep the computer from sleeping when it's idle, but not when the user
closes a laptop lid or pushes the power button.

(2) Use the powercfg command (or PowerDuplicateScheme(), etc.) to create a temporary power scheme
with sleep and hibernation disabled, then restore the original power scheme after the command has
finished.  See for example [3], which does this (that particular implementation needs work -
powercfg needs different options in Windows XP and earlier).

Which approach do people think is better?  Or is there another, better way?

#2 seems more fragile, in the sense that it will fail to restore the original power scheme if the
parent process is killed before it finishes executing.  By contrast, #1 doesn't have any cleanup to
do at the end, so there's no harm done if the parent process is killed.

#2 will prevent the user from manually putting the computer to sleep or hibernating, but I'm not
sure if that's an advantage.  Users won't necessarily know why their computer won't go to sleep when
they tell it to, or why after a while it starts to work again.  Whether it's worthwhile may depend
on how important it is for the requested command to finish - although that still isn't guaranteed
under #2, since the user can still shut the machine down.

Finally, it seems likely that #1 needs lower privileges than #2, although I'm not sure about that
yet.

[1]
http://www.vistax64.com/general-discussion/222308-vista-goes-sleep-while-burning-dvd.html#post1022013
[2] http://msdn.microsoft.com/en-us/library/aa373208(v=vs.85).aspx
[3] http://superuser.com/questions/90415/prevent-sleep-in-scripts/90432#90432


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple



More information about the Cygwin mailing list