Author Topic: Detect Windows system closing my script  (Read 461 times)

gibberish

  • Newbie
  • *
  • Posts: 12
Detect Windows system closing my script
« on: April 24, 2018, 04:22:00 pm »
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

  • Tech Support
  • *****
  • Posts: 2595
    • WinBatch
Re: Detect Windows system closing my script
« Reply #1 on: April 25, 2018, 07:35:29 am »
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.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


gibberish

  • Newbie
  • *
  • Posts: 12
Re: Detect Windows system closing my script
« Reply #2 on: April 25, 2018, 07:41:42 am »
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

  • Tech Support
  • *****
  • Posts: 2595
    • WinBatch
Re: Detect Windows system closing my script
« Reply #3 on: April 25, 2018, 11:11:57 am »
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.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


td

  • Tech Support
  • *****
  • Posts: 2595
    • WinBatch
Re: Detect Windows system closing my script
« Reply #4 on: April 25, 2018, 01:03:04 pm »
Also check out the SERVICE_CONTROL_LOGOFF and SERVICE_CONTROL_PRESHUTDOWN  notices, if you are working with a service.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


gibberish

  • Newbie
  • *
  • Posts: 12
Re: Detect Windows system closing my script
« Reply #5 on: April 25, 2018, 01:27:18 pm »
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

  • Tech Support
  • *****
  • Posts: 2595
    • WinBatch
Re: Detect Windows system closing my script
« Reply #6 on: April 25, 2018, 10:07:27 pm »
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.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates