如果批处理命令没有在给定的时间内终止,则停止该命令 [英] Stop a batch command if it doesn't terminate in a given period of time

查看:135
本文介绍了如果批处理命令没有在给定的时间内终止,则停止该命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的批处理文件,该文件一个接一个地运行一堆PHP文件.有时,其中一些php文件会挂起,并且当这种情况发生时,整个批处理文件将停止执行此后要发送的文件.

I have a simple batch file, which runs a bunch of PHP files, one after the other. Sometimes, some of these php files hangs, and when this happens, the whole batch file stops from executing the files that are comming after this.

有没有一种方法可以停止运行该命令,并在给定的时间段后继续执行批处理脚本中的下一个命令?

Is there a way, to stop running the command, and proceed to the next command after a given period of time, in a batch script?

到目前为止,我的批处理文件仅包含大约800行,其命令与以下命令类似:

My batch file only consists till now around 800 lines with command simmilar to the ones below:

php72 ../simulation.php --version 0.9.0.4 --hashsimmilar false --thinkahead 0 --detailed 0 --outfile catacombs.outfile.csv --workingdir "C:/xampp/htdocs/rpg_prog/" --customfight 1,2,,,,1,,,1,,0,0,0,Catacombs1
php72 ../simulation.php --version 0.9.0.4 --hashsimmilar false --thinkahead 1 --detailed 0 --outfile catacombs.outfile.csv --workingdir "C:/xampp/htdocs/rpg_prog/" --customfight 1,2,,,,1,,,1,,0,0,0,Catacombs1
php72 ../simulation.php --version 0.9.0.4 --hashsimmilar false --thinkahead 2 --detailed 0 --outfile catacombs.outfile.csv --workingdir "C:/xampp/htdocs/rpg_prog/" --customfight 1,2,,,,1,,,1,,0,0,0,Catacombs1

例如,如果以上列表中的第二行挂起,则整个批处理文件将挂起,而第三行将永远不会执行.

If for example, the second line in the above list hangs, the whole batch file will hang, and the third line will never be executed.

推荐答案

鉴于没有其他php72.exe进程,您可以在每个php72调用之前加上start "" /B前缀,并在每个php72调用之后加上以下几行(如果您不想删除/F选项,强行杀死任务):

Given there are not any other php72.exe processes, you could prefix each php72 call by start "" /B and put after each php72 call the following lines (remove the /F option if you do not want to kill the tasks forcefully):

timeout /T 10 /NOBREAK > nul
taskkill /IM "php72.exe" /F > nul 2>&1

>后缀只是避免引发任何(错误)消息.

The > suffixes just avoid any (error) messages to be thrown.

不必多次复制以上行,也可以将以下代码放在批处理文件的顶部:

To not have to copy the above lines multiple times, you could also place the following code on top of your batch file:

@echo off
rem // Read all lines from this batch file that begin with `php72 ` and iterate over them:
for /F "delims=" %%C in ('
    findstr /BIC:"php72 " "%~f0"
') do (
    rem // Execute the currently iterated `php72` command line:
    start "" /B %%C
    rem // Wait for some time:
    timeout /T 10 /NOBREAK > nul
    rem // Kill the `php72` process (or actually all of them) if still running:
    taskkill /IM "php72.exe" /F > nul 2>&1
)
rem // Avoid to fall into the `php72` command lines another time:
exit /B

rem // These are your lines:
php72 ../simulation.php --version 0.9.0.4 --hashsimmilar false --thinkahead 0 --detailed 0 --outfile catacombs.outfile.csv --workingdir "C:/xampp/htdocs/rpg_prog/" --customfight 1,2,,,,1,,,1,,0,0,0,Catacombs1
php72 ../simulation.php --version 0.9.0.4 --hashsimmilar false --thinkahead 1 --detailed 0 --outfile catacombs.outfile.csv --workingdir "C:/xampp/htdocs/rpg_prog/" --customfight 1,2,,,,1,,,1,,0,0,0,Catacombs1
php72 ../simulation.php --version 0.9.0.4 --hashsimmilar false --thinkahead 2 --detailed 0 --outfile catacombs.outfile.csv --workingdir "C:/xampp/htdocs/rpg_prog/" --customfight 1,2,,,,1,,,1,,0,0,0,Catacombs1

当然,您可以将php72命令行放在单独的文件中,然后需要相应地修改for /F循环:

Of course you can have the php72 command lines in a separate file, then the for /F loop needs to be adapted accordingly:

rem // Specify appropriate text file:
for /F "usebackq delims=" %%C in ("D:\path\to\file.txt") do (
    rem // Same loop body as above...
)

这篇关于如果批处理命令没有在给定的时间内终止,则停止该命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆