2

On my Win10 1909 PC i have recently discovered that if I shutdown / restart while a Batch File is waiting on a timeout command, then rather than it terminating outright, instead only the timeout command terminates, and the Batch File wil continue on with its next few instructions.

So for example if I run the Batch File:

 timeout /nobreak 60000
 echo %time%>> "%UserProfile%\Desktop\ThisShouldntExist.txt"

and restart, I will find a "ThisShouldntExist" text file on the desktop at next logon.

I might have several Batch Files running that are waiting in a timeout phase, and don't like the thought of them carrying out some of their post-timeout instructions in an unstable environment, should I forget to manually close them at shutdown.

My queries are, first is this a known thing that happens with everyone?

Second, can you think of some lines I could add to my shutdown Batch script, that would close all other runnin Batch Files other than itself, before it shuts the PC down? I have NirCMD if it helps

EDIT I already use force in my shutdown script. The shutdown process stil only initially ends the timeout countdown, leaving the Batch File free to carry out further instructions for another second or more

1
  • 1
    This is the expected behavior since shutdown doesn't shut down programs it shuts down windows. But most programs can detect a shutdown (or windows detects open programs) and asks you to close them, except with shutdown /f Commented Nov 22, 2020 at 12:09

2 Answers 2

0

Batch Files not terminating correctly at shutdown

if I shutdown / restart while a Batch File is waiting on a timeout command, then rather than it terminating outright, instead only the timeout command terminates

This is expected behaviour as a normal shutdown allows programs time to terminate gracefully. If, for example, you have a document open in an editor the system warns you so you can save the document.

If you want to force an immediate shutdown then use the /f option:

/f Force running applications to close.
   This will not prompt for File-Save in any open applications.
   so will result in a loss of any unsaved data.

Source Shutdown - Shutdown the computer - Windows CMD - SS64.com

1
  • I already use force in my shutdown script. The shutdown process stil only initially ends the timeout countdown, leaving the Batch File free to carry out further instructions for another second or more
    – TechHorse
    Commented Nov 22, 2020 at 9:39
0
@echo off 

rem :: code until 
rem :: to kill any process

set "_kill=cmd.exe timeout.exe"
set "_title=Keep_Yourself_Alive"
<con: title <nul & title %_title%

pushd %__AppDir__% && for /f tokens^=1^,3delims^=^" %%i in ('
tasklist /v /fo csv ^|findstr /i "%_kill%"^|findstr /vi "%_title%"
')do 2>nul <con: taskkill.exe /f /pid %%j >nul | echo\Bye Bye %%j:%%i

<con: rem./ & shutdown /and /your /flags /here & popd 
rem :: more code came here, after to kill process ... 

  • I never realized that Shutdown were attentive to waiting for the Timeout good to know...

1. Assign a unique Title to your bat:

@echo off 
Title Keep_Yourself_Alive

2. List with For /F the process PID of each CMD and Timeout with Tasklist and Finsdtr:

for /f tokens^=3delims^=^" %%i in ('tasklist /v /fo csv|findstr "cmd.exe timeout.exe"... 

3. One additional Findstr can be used to list only PIDs that does not match your unique bat Title

... ^|findstr /vi "Keep_Yourself_Alive" ... 

4. Use Taskkill with PID to kill the entire CMD.exe + Timeout process (if running*)

... ')do taskkill /F /PID %%i

Additional resources:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .