Page 1 of 1 1
Topic Options
#150780 - 2005-10-28 08:52 PM Detect OS/Service shutdown in a loop
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
I haven't done any research on this yet but I was hoping someone has.

I have SRVANY running a kix script that loops continuously. Sometimes on system shutdown, why not all of the time, I don't know, I get the kix error audible beep. It hasn't negatively affected anything yet and there's a very slim chance that it ever would, but it's just not professional.

Q. Is it possible to detect a system shutdown in my loop and exit before the process is terminated and the beep occurs. (without wrapping the kix in anything)
_________________________
-Jim

...the sort of general malaise that only the genius possess and the insane lament.

Top
#150781 - 2005-10-28 09:11 PM Re: Detect OS/Service shutdown in a loop
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
I don't think KiX can do it natively but a KiXform might be able to, or Shawn might be able to add that as a feature.

WM_QUERYENDSESSION is what is sent to all applications, but no way for KiX to get that and let you know.

Here is an article for Shawn if it needs to go that way.

Making a Windows Forms app respond to System Shutdown
http://www.stevex.org/cs/blogs/dottext/articles/155.aspx
 

Top
#150782 - 2005-10-28 09:19 PM Re: Detect OS/Service shutdown in a loop
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
I wanted to stay away from wrapping it and unfortunately kixforms isn't an option...

I used this process before and srvany would stop the service with grace. I don't quite understand why it's only intermittent now either. Perhaps b/c the looping frequency is shorter? Current loop processing vs. sleeping. Either way I figure it would always beep, but that's definitely not the case.
_________________________
-Jim

...the sort of general malaise that only the genius possess and the insane lament.

Top
#150783 - 2005-11-08 01:20 AM Re: Detect OS/Service shutdown in a loop
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Alrighty. I've got something working, but not sure how to implement it.

Have a look see at me code then there.
Code:
Break On
$nul=SetOption("Explicit","On")
$nul=SetOption("ASCII","On")

Dim $err
Dim $objWMI,$objEvent,$objHandle
Dim $strWQLQuery,$strType
Dim $fileOutput

$fileOutput = "c:\sd.log"
$err=0

$strWQLQuery = "SELECT * FROM Win32_ComputerShutdownEvent"
$objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!root\cimv2")
$objEvent = $objWMI.ExecNotificationQuery($strWQLQuery)

$nul=RedirectOutput($fileOutput)

While $err=0
? @Date + " " + @Time + " " + "Waiting for event..."
$objHandle=$objEvent.NextEvent
If $objHandle.Type = 0
$strType="Logoff"
? @Date + " " + @Time + " " + $strType + " Event Detected."
Else
$strType="Shutdown/Reboot"
? @Date + " " + @Time + " " + $strType + " Event Detected."
$err=1
ShutdownServices()
EndIf
Loop

? @Date + " " + @Time + " " + "Graceful shutdown."
Exit 0


Function ShutdownServices()
? @Date + " " + @Time + " " + "Stopping IISAdmin..."
Shell('cmd /c "Net Stop IISAdmin"')
? @Date + " " + @Time + " " + "Completed with EC:" + @Error + " SC:" +@Serror
EndFunction



Rather than call the shutdownservice fn, I'd like to directly let the other running kix script know that it's time to exit its loop. Problem is that I found no way to run the above asyncronously without a Sink_. And the Sink_ wouldn't work either b/c the vbscript would still need to inform the running kix script that it's time to quit.

I'd hate to scan a file or use a file as a semaphore, but I'm not seeing any other options... Any ideas out there?
_________________________
-Jim

...the sort of general malaise that only the genius possess and the insane lament.

Top
#150784 - 2005-11-08 05:47 AM Re: Detect OS/Service shutdown in a loop
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Event sinks are not supported by KiXtart.
_________________________
There are two types of vessels, submarines and targets.

Top
#150785 - 2005-11-08 11:26 AM Re: Detect OS/Service shutdown in a loop
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

I'd hate to scan a file or use a file as a semaphore, but I'm not seeing any other options... Any ideas out there?




I'm not too sure what you are asking for - you might need to explain a little more.

If you are just looking for a way that two process can communicate without using a file then two methods spring to mind.

  1. Use the registry. The two scripts agree on a sub-key or value and then use it to communicate. This is excellect for two-way communication, especially when you need a high degree of handshaking.
  2. Execute a process with fixed option values. Excellent if you don't want to leave any traces, but much harder to code and may only be supportable with XP and 2003.


The first option is an obvious method and very easy. The second option is a little more complicated.

The way it works is simple. In the "talking" script you run a command like:
Code:
Shell '"'+%COMSPEC%+'" /C "SLEEP 9999 && REM IPC: Some Message'



In the "listening" script you do:
Code:
Break ON
$=SetOption("Explicit","ON")
$=SetOption("WrapAtEOL","ON")

Dim $oWMI,$oProcess,$oProcesses,$iQuit,$sMessage
Dim $sSentinel,$iTerminate

$sSentinel="REM IPC: "

$oWMI=GetObject("Winmgmts:{impersonationLevel=impersonate}")

While Not $iQuit
Sleep $iTerminate $iTerminate=0 ; Give terminated processes a change to close.
$oProcesses=$oWMI.ExecQuery('Select CommandLine from Win32_Process')
If @ERROR "2 " @ERROR " " @SERROR ? EndIf
For Each $oProcess in $oProcesses
If InStr($oProcess.CommandLine,$sSentinel)
$sMessage=$oProcess.CommandLine
$sMessage=SubStr($sMessage,InStr($sMessage,$sSentinel)+Len($sSentinel))
Select
Case $sMessage="QUIT"
$iQuit=1
Case "Not handled"
"Unhandled message from talker: "+$sMessage+@CRLF
EndSelect
$=$oProcess.Terminate
$iTerminate=2
EndIf
Next
Loop

Exit 0



Now, you can tidy up the select clause and improved the wait for the terminated process, but I'm sure that you get the idea.

If you abstract the "talk" and "listen" processes into functions it makes the whole thing fairly easy.

The only drawback is that "CommandLine" may not be universally supported.

Top
#150786 - 2005-11-08 04:38 PM Re: Detect OS/Service shutdown in a loop
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Richard,

Thanks for the ideas. I guess I need to ask which method will impose the least overhead? I like the registry idea for its simplicity, but b/c I'm monitoring for a system shutdown I'll need to poll whatever source very frequently.
_________________________
-Jim

...the sort of general malaise that only the genius possess and the insane lament.

Top
#150787 - 2005-11-08 05:13 PM Re: Detect OS/Service shutdown in a loop
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
A registry read will definately have lower overhead than any WMI-oriented solution.

Be sure to clean up registry entries when you start.

If one script starts the other then you can get them to negotiate a unique key or value name at startup so that you don't clash with any other process or pick up artifacts from previous failed instances.

Top
Page 1 of 1 1


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1452 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.187 seconds in which 0.127 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org