Terminate Batch Job skip
#44
andry81
started this conversation in
Show and tell
Replies: 1 comment
-
|
There is exist one builtin method by using call-with-label. @echo off
setlocal
rem DOES work in case of CTRL+BREAK
start /WAIT "" timeout.exe /T -1 /NOBREAK & call :SETERR
echo ERRORLEVEL=%ERRORLEVEL%
exit /b
:SETERR
exit /bResult:^CERRORLEVEL=-1073741510Note This method can not be used in case of an automation, because Automation through the @echo off
setlocal
call "%%~dp0__init__\__init__.bat" || exit /b
rem DOES work in case of CTRL+BREAK
start /WAIT "" timeout.exe /T -1 /NOBREAK & call "%%CONTOOLS_ROOT%%/std/errlvl.bat"
echo ERRORLEVEL=%ERRORLEVEL%Result:^CERRORLEVEL=-1073741510 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The
Terminate Batch Jobmessage issued by thecmd.exein case if a sub process you handled to close (by CTRL-C or what ever takes) is returned-1073741510.You can replace the exit code to suppress the interruption.
Tests:
https://github.com/andry81/contools--debug/tree/HEAD/test_skip_terminate_batch_job_by_filter_exit_code_01.bat
https://github.com/andry81/contools--debug/tree/HEAD/test_skip_terminate_batch_job_by_filter_exit_code_02.bat
https://github.com/andry81/contools--debug/tree/HEAD/test_skip_terminate_batch_job_by_filter_exit_code_03.bat
Results:
test_skip_terminate_batch_job_by_filter_exit_code_1.bat:
^CERRORLEVEL=9009test_skip_terminate_batch_job_by_filter_exit_code_2.bat:
ERRORLEVEL=255test_skip_terminate_batch_job_by_filter_exit_code_3.bat:
Note
You can't put
cmd.exe /c exit -1073741510into a script, because in that case it will be asked for the interruption instead of the caller script.Note
The exit code after the interruption is not always determined and can be overriden by the
cmd.exelike in the example 2, 3 (not-1073741510nor9009).Note
You have to handle the exit code replacement from each sub process in each script.
To improve the usage you can create a variable:
Warning
The
startprefix does not trigger the command line after the||operator in case of CTRL+BREAK or close console window using UI. But works with the&operator.Test:
https://github.com/andry81/contools--debug/tree/HEAD/test_skip_terminate_batch_job_by_filter_exit_code_04.bat
Result:
^CERRORLEVEL=9009Note
You can not minimize
call "%%SystemRoot%%\System32\cmd.exe" /S /c "@if %%ERRORLEVEL%% EQU -1073741510 (exit 9009) else exit %%ERRORLEVEL%%"down tocall "%%SystemRoot%%\System32\cmd.exe" /c @exit %%ERRORLEVEL%%"because of CTRL+BREAK case or close through the console UI button (which is the same).Note
If a process does return the interruption exit code (
-1073741510), then you can handle the interruption using9009code ('...' is not recognized as an internal or external command, operable program or batch file.message) as a replacement. As said before this is not always possible due to different return codes.Test:
https://github.com/andry81/contools--debug/tree/HEAD/test_skip_terminate_batch_job_by_filter_exit_code_05_ctrl_c_only_1.bat
Now, only CTRL+C skips the message, when CTRL+BREAK and UI close button does keep request the termination.
Result:
^C^CTerminate batch job (Y/N)?To automate the usage you can split the command line into 2 variables:
https://github.com/andry81/contools--debug/tree/HEAD/test_skip_terminate_batch_job_by_filter_exit_code_06_ctrl_c_only_2.bat
Result:
^CTerminate batch job (Y/N)?Note
Example 5 and 6 has a difference. The latter does print
^Cup on CTRL+BREAK and UI close button only once.Note
The
@CMD_SKIP_TERMINATE_BATCH_JOBand:CALL_CMD_SKIP_TERMINATE_BATCH_JOBvariables has a difference. The former works for both CTRL+C and CTRL+BREAK including UI close button. When the latter works only for CTRL+C.Note
For example, the
set ":CMD=cmd.exe"call %:CMD% /kin a script won't work due to early parse stage for the labels:The system cannot find the batch label specified - CMD.Beta Was this translation helpful? Give feedback.
All reactions