Jump to content


Script than: on system shutdown, perform an action


  • Please log in to reply
13 replies to this topic

#1 JamiePhonic

JamiePhonic
  • Guests

Posted 10 July 2012 - 11:45 PM

i have a windows server that runs a minecraft server, since im lazy, i don't bother going to start, shutdown, i just press the power button. obviously this isn't good for the minecraft server because it doesn't get a chance to stop properly before windows kills the process.

basically i need a script that, on system shutdown, it aborts the shutdown, selects the minecraft server Console window and types stop before waiting 10 seconds and then shutting down the system.

#2 dylan904

dylan904
  • Members
  • 706 posts

Posted 10 July 2012 - 11:55 PM

AFAIK, when you press the power button, it's game over.

#3 JamiePhonic

JamiePhonic
  • Guests

Posted 11 July 2012 - 12:04 AM

there is a way to do it. it has something to do with http://www.autohotke...ands/OnExit.htm
i just cant figure out the code

#4 Ohnitiel

Ohnitiel
  • Members
  • 664 posts

Posted 11 July 2012 - 12:10 AM

When you press the shutdown button, windows closes the process that are open. OnExit only works on a normal script exit.

#5 JSLover

JSLover
  • Members
  • 920 posts

Posted 11 July 2012 - 12:19 AM

i just press the power button.

...when you do that, does the computer "begin shutting down" or just "immediately lose power"?

Most power buttons on computers these days are more like a "soft power button", they inform the OS that you pressed power & give the OS the choice of what to do (which means the OS can shutdown cleanly -- or not shutdown at all).

If pressing the power button causes an "immediate loss of power", then it is "Game Over"...you have a computer with a "dumb" power button & there is nothing you can do, that includes being able to press the power button -- Also, if this is the case, STOP DOING THAT IMMEDIATELY...sudden power loss is REALLY BAD for a computer!

However, if not (if you have a computer with a "smart" power button), then a script can help you.

AFAIK, when you press the power button, it's game over.

...not always, at least not with newer computers...& even semi-old computers (with a "smart" power button) do as mentioned above.

#6 JamiePhonic

JamiePhonic
  • Guests

Posted 11 July 2012 - 12:31 AM

the system im using is an "HP Compaq d530 Small Form Factor Desktop PC"
when i press the power button, windows starts to shutdown the same way it would if i had gone to start==>shutdown.

#7 MasterFocus

MasterFocus
  • Moderators
  • 4158 posts

Posted 11 July 2012 - 12:38 AM

Search for "shutdown message".

Related:
- <!-- l --><a class="postlink-local" href="http://www.autohotkey.com/community/viewtopic.php?t=73680">viewtopic.php?t=73680</a><!-- l --> !!
- <!-- l --><a class="postlink-local" href="http://www.autohotkey.com/community/viewtopic.php?t=26304">viewtopic.php?t=26304</a><!-- l -->
- <!-- l --><a class="postlink-local" href="http://www.autohotkey.com/community/viewtopic.php?p=338084#p338084">viewtopic.php?p=338084#p338084</a><!-- l -->

#8 dylan904

dylan904
  • Members
  • 706 posts

Posted 11 July 2012 - 01:30 AM

Yes it seems like this should work as dmg posted...
OnMessage(0x11, "WM_QUERYENDSESSION")

Return ;Ends auto-execute section.



WM_QUERYENDSESSION()

{

   Do what you gotta do

}

Return


#9 JamiePhonic

JamiePhonic
  • Guests

Posted 11 July 2012 - 01:21 PM

Ok! so i got the code working but there's a problem.
OnMessage(0x11, "WM_QUERYENDSESSION")
Return ;Ends auto-execute section.

WM_QUERYENDSESSION()
{
If WinExist("ahk_class ConsoleWindowClass")
    WinActivate
	sleep, 250
	sendinput, stop{enter}
	sleep 5000
}
Return
the problem with this code is that because the minecraft server is one of only 5 program running, windows kills it before the script can do its job.
what ive decided to do is go into the power options and set:
When i press the power button on my computer:
Do nothing.
so that windows no longer shuts down when i press the power button.
im wondering if there's a way for autohotkey to detect that ive pressed the power button and then run the script before initiating the shutdown using something like "shutdown -s -t0" (CMD shutdown code) or autohotkeys own version of it?

#10 dylan904

dylan904
  • Members
  • 706 posts

Posted 11 July 2012 - 01:58 PM

Maybe if you changed some Windows priorities around it would make a difference?

#11 JamiePhonic

JamiePhonic
  • Guests

Posted 11 July 2012 - 02:17 PM

Maybe if you changed some Windows priorities around it would make a difference?

there's not enough processes to make a difference.
the server runs: a minecraft server, PS3 media server and uTorrent. that's it

#12 JamiePhonic

JamiePhonic
  • Guests

Posted 11 July 2012 - 03:38 PM

never mind, i figured it out after spending 3 hours trolling the forums, here is the code

DllCall("kernel32.dll\SetProcessShutdownParameters", UInt, 0x4FF, UInt, 0) ;make windows kill this process first
OnMessage(0x11, "WM_QUERYENDSESSION")
return

WM_QUERYENDSESSION(wParam, lParam)
{
    ENDSESSION_LOGOFF = 0x80000000
    If  WinExist("ahk_class ConsoleWindowClass") ;if a cmd window is open,
    	{
	WinActivate ;switch to it
	sleep, 500 ;wait half a second
	sendinput, stop{enter} ;type stop, press enter
	sleep 5000 ;wait 5 seconds
	return true  ; Tell the OS to allow the shutdown/logoff to continue.
	exitapp ;quit the program
	}
    else ; if the cmd window is already closed,
	return true ; Tell the OS to allow the shutdown/logoff to continue.
	exitapp ;quit the program
}


#13 Bartimus

Bartimus
  • Members
  • 203 posts

Posted 11 July 2012 - 03:40 PM

what ive decided to do is go into the power options and set:

When i press the power button on my computer:
Do nothing.
so that windows no longer shuts down when i press the power button.
im wondering if there's a way for autohotkey to detect that ive pressed the power button and then run the script before initiating the shutdown using something like "shutdown -s -t0" (CMD shutdown code) or autohotkeys own version of it?



How about this idea
keep the power management set the way you have it (When you press the power button, nothing happens), but now create a Hotkey for shutting the PC down instead

[color=#BF0000]^!s::[/color]

{
If WinExist("ahk_class ConsoleWindowClass")
    WinActivate
	sleep, 250
	sendinput, stop{enter}
	sleep 5000
}
[color=#BF0000]Shutdown 8[/color]


#14 JamiePhonic

JamiePhonic
  • Guests

Posted 12 July 2012 - 05:06 PM

How about this idea
keep the power management set the way you have it (When you press the power button, nothing happens), but now create a Hotkey for shutting the PC down instead

[color=#BF0000]^!s::[/color]

{
If WinExist("ahk_class ConsoleWindowClass")
    WinActivate
	sleep, 250
	sendinput, stop{enter}
	sleep 5000
}
[color=#BF0000]Shutdown 8[/color]


that's no use. its a headless server. (no keyboard, mouse or monitor. its all configured over remote desktop)