Detect Windows system closing my script

Started by gibberish, April 24, 2018, 04:22:00 PM

Previous topic - Next topic

gibberish

I have a TSR that monitors some files. When Windows is shutdown, the script is closed and I would like to annotate a file with a shutdown message.

How do I detect the script is being closed by Windows?
Must I do anything in particular to steal a few extra microseconds for script to write to log file as it exits?


td

I haven't read the acronym "TSR" for maybe 20 years... You didn't provide enough context so a few assumptions need to be made but generally, you don't need to do anything when the OS terminates a WinBatch process.   The OS notifies the process that it is about to be terminated and the process does what it needs to do and signals the OS that it is ready.  The OS then sends a second message to the process telling it to terminate and the process terminates itself.     If a process is poorly written or has run amock so that it doesn't respond to the termination request, the OS just terminates the process after a preordained period of time or at least ask the user if it should continue shutting down.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

gibberish

Apologies for not providing enough detail.

I need to write some logic into the script itself so that when it is closed by Windows it knows to write something to a log file.

Perhaps this is dead simple and I just cannot recall how to do it. What IntControl or etc will trigger when the script is being closed? How can my script detect this event?

Thanks!

td

WinBatch doesn't have a mechanism for doing what you are requesting for scripts running as regular user processes.  The only thing provided is IntControl 12 which can be used to display a message or get the OS to display a message when Windows is attempting to terminate the WinBatch process.  However, if a compiled script is running as a native service, the SCM will send the service a "SERVICE_CONTROL_SHUTDOWN" notice when the system is about to shut down.  The service can perform shut down tasks when it receives this notification. 

See  "Home->WinBatch Users Guide->Run as a Native Service" in the Consolidated WIL Help file for more information.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

Also check out the SERVICE_CONTROL_LOGOFF and SERVICE_CONTROL_PRESHUTDOWN  notices, if you are working with a service.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

gibberish

Thanks much for the ideas, Mr. T. I see now what you mean about the info you were looking for in my original question -- my script is not compiled and not running as a service. Just a vanilla .wbt launched via startup group in Win7.

As a workaround, I guess I can start another logfile and update it each loop with a timestamp, and if the timestamp is older than x seconds assume the script was shutdown at that point. Then I'll check for that on script start and annotate the official logfile accordingly. For some reason I thought there was a way of detecting "On WinClose" or some such -- but WB does so many other amazing things. There's always a way to do what I want, even if the occasional kludge is required.

Many thanks !

td

WinBatch has several mechanisms for doing pre-process-termination processing when a user ends a script by some interaction with the script - pressing a dialog cancel button for example.   But as previously mentioned, it does not have a mechanism for that kind of processing when the termination is the result of a user session ending or the system shutting down unless it is running as a native service.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade