首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果批处理命令在给定时间内未终止,则停止该命令

问如果批处理命令在给定时间内未终止,则停止该命令
EN

Stack Overflow用户
提问于 2019-02-05 21:25:04
回答 1查看 42关注 0票数 0

我有一个简单的批处理文件,它一个接一个地运行一堆PHP文件。有时,这些php文件中的一些挂起,当发生这种情况时,整个批处理文件将停止执行在此之后开始的文件。

是否有办法停止运行命令,并在给定时间后,在批处理脚本中继续执行下一个命令?

到目前为止,我的批处理文件仅包含大约800行命令simmilar,如下所示:

代码语言:javascript
复制
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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-05 22:14:31

由于没有任何其他php72.exe进程,您可以在每个php72调用前加上start "" /B前缀,并将每个php72调用放在下面的行之后(如果不想强行终止任务,请删除/F选项):

代码语言:javascript
复制
timeout /T 10 /NOBREAK > nul
taskkill /IM "php72.exe" /F > nul 2>&1

>后缀只需避免抛出任何(错误)消息。

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

代码语言:javascript
复制
@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循环:

代码语言:javascript
复制
rem // Specify appropriate text file:
for /F "usebackq delims=" %%C in ("D:\path\to\file.txt") do (
    rem // Same loop body as above...
)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54543272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档