Archive

Archive for the ‘Backups’ Category

AutoHotKey Script to enter windows’ ‘Away Mode’ until a process completes

August 13th, 2012 No comments

For whatever reason, I don’t want my workstations initiating this backup. I have a server that calls out in the middle of the night to initiate an ssh-encrypted rsync backup operation which takes a random amount of time to complete. I need to have workstations wake up and then not go to sleep while this backup happens and Away Mode keeps the workstations using the least power possible.

This AutoHotKey thread gave me the vital information I needed to set away mode:

Win7 adaptation of intercept hibernation script

That was very helpful and scarce info so I wanted to expand on it and give something back.

For the rest I use the Nirsoft utility nircmd. I’m sure those functions could be easily implemented natively in AHK but I needed quick and dirty.

There’s a bunch of sound beeps I used for testing and debugging but they do nothing when run as a background task. Away mode supposedly mutes the speaker by default. There might also be Session 0 issues with it.

Here’s what the script does:

-wake up the laptop from sleep or hibernate (if it’s plugged in) – The task scheduler takes care of this.

-set the pc to “away mode”:
The system appears to be asleep, but it isn’t.
The screen might turn off, but the system won’t automatically go to sleep again until I release away mode.
(In away mode, the system can still sleep or hibernate if power runs out, or a user presses a button or something, but that’s ok.)

-turn off the monitor
(this doesn’t work yet… stupid microsoft and their session 0 isolation crap seem to prevent traditional monitor sleep tricks, another post in itself)

-wait 10 minutes for the rsync operation to get started, to account for any minor clock/schedule differences.

-after the 10 minutes, it waits for the rsync process to finish, taking as much time as needed.
(I set up to 8 hrs before task scheduler forcefully kills this script)
If there is no rsync process we continue on. (rsync could start and finish within the first 10 minutes, or never happen at all)

-wait an additional 5 minutes, then release away mode

-immediately put the system back into sleep mode, or hibernate, whatever.

That’s it. Here’s the script:

; Wait for rsync to complete, waiting at least 10 minutes minimum
; keep computer "asleepish"
; go directly to sleep afterwards

; 20120810

; IGNORE skips the dialog box and leaves the old instance running. In other words, attempts to launch an already-running script are ignored.

#SingleInstance ignore

ES_AWAYMODE_REQUIRED:=0x00000040
ES_CONTINUOUS:=0x80000000
ES_SYSTEM_REQUIRED:=0x00000001

; Tell the OS to enable awaymode and prevent automatic sleep
DllCall("SetThreadExecutionState","UInt",ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED)

; turn off monitor immediately (doesn't work when running in Session 0)

Run "C:\Utils\Revealers\nircmdc-x64.exe" cmdwait 1000 monitor async_off,,Hide

; Sleep for 10 mins, to allow rsync to begin
; beep once and a while

SoundBeep, 450, 500

Loop, 10
{
Sleep, 60000
SoundBeep, 550, 500
SoundBeep, 650, 500
}

SoundBeep, 850, 500
SoundBeep, 750, 500


; Wait for rsync to complete
; exits immediately if rsync process does not exist

;"C:\Utils\Revealers\nircmdc-x64.exe" waitprocess rsyncd.exe

Process WaitClose, rsync.exe

SoundBeep, 750, 500
SoundBeep, 850, 500

; Deactivate awaymode and allow system to sleep normally again
DllCall("SetThreadExecutionState","UInt",ES_CONTINUOUS)


; sleep the system 5 minutes later

Loop, 5
{
Sleep, 60000
SoundBeep, 650, 500
SoundBeep, 550, 500
}

SoundBeep, 750, 500
SoundBeep, 850, 500


; http://www.autohotkey.com/docs/commands ... tm#Suspend

Run "C:\Utils\Revealers\nircmdc-x64.exe" cmdwait 500 standby,,Hide

; quit and exit autohotkey
; will this get to finish before we sleep?? My tests say yes. ymmv

ExitApp
Categories: Backups, Programming, Windows 7 Tags: