How to add a stop execution command in a batch file

两盒软妹~` 提交于 2020-01-15 03:52:25

问题


I have to create a batch file which will perform some operation but I need to add some commands so that it will stop execution if it exceeds 30 minutes of execution time.

Can anybody help? I am new to batch scripting.


回答1:


Rather you need to call your .bat from another one and check after 30 minutes if it's still running

@echo off

set "my_bat=E:\MyBat.bat"

for /f  "delims=" %%a in ('wmic process call create "%my_bat%" ^|find "ProcessId"') do (
    for /f "tokens=2 delims=;= " %%# in  ("%%a") do set "PID=%%#"
)

::echo %PID%
::sleep for 30 minutes
ping 1.1.1.1 -n 1 -w 1800000 > nul

::kill the bat
tskill %PID% >nul 2>&1

you need to change the location of my_bat to the yours with full path.



来源:https://stackoverflow.com/questions/28190686/how-to-add-a-stop-execution-command-in-a-batch-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!