Stop Windows 7 PC from Hibernation When Ruby Runs

I need to run a Ruby application on a client computer. As a rule, it takes several days to complete (copies large backup files). The problem is that if sleep is on, it will interrupt the application. If the computer will not stay for weeks until my next visit. Is there a way to prevent sleep at runtime and let Windows sleep after?

Any crazy ideas are welcome; -)

+5
source share
3 answers

I don't really like it on the Windows platform, but the following approach seems logical:

  • Windows, , / .
  • , .
  • .
  • /

, , .

, , .

, !

0

SetThreadExecutionState WinAPI, , , , .
- :

require 'Win32API'
ES_AWAYMODE_REQUIRED = 0x00000040
ES_CONTINUOUS = 0x80000000
ES_DISPLAY_REQUIRED = 0x00000002
ES_SYSTEM_REQUIRED = 0x00000001
ES_USER_PRESENT = 0x00000004
func = Win32API.new 'kernel32','SetThreadExecutionState','L'

func.call(ES_CONTINUOUS|ES_SYSTEM_REQUIRED)
# doing your stuff...
func.call(ES_CONTINUOUS)


- ( , )
+6

, Ruby , ,

puts `command here`

, ,

c:\windows\system32\powercfg.exe -change -standby-timeout-ac 0

puts `c:\\windows\\system32\\powercfg.exe -change -standby-timeout-ac 0`

\ . , . , .

- , . , , . , 10 ,

puts `c:\\windows\\system32\\powercfg.exe -change -standby-timeout-ac 10`

So, you can disable sleep mode at the beginning of the script and enable sleep again.

Hope this suits you!

+2
source

All Articles