运行和终止时出错然后在特定时间段运行新的批处理文件

运行和终止时出错然后在特定时间段运行新的批处理文件

运行和终止时出错,然后在特定时间段内运行另一个批处理文件。尝试同时运行超时和 CALL,但我猜我的行中有错误,所以它没有通过。由于我的 .bat 文件一旦运行就不会自行完成,所以我不能只将超时放在它下面的另一行上。尝试使用 | 但它也不会这样做

@echo off
set /a maxLoop=999999
set /a loopCount=%maxLoop%
for /l %%x in (1, 1, %maxLoop%) do (
   CALL c:\rclone\rclone1.bat | timeout 21600
   wmic process where name="rclone1.exe" call terminate > nul
   CALL c:\rclone\rclone2.bat | timeout 21600
   wmic process where name="rclone2.exe" call terminate > nul
   CALL c:\rclone\rclone3.bat | timeout 21600
   wmic process where name="rclone3.exe" call terminate > nul
   CALL c:\rclone\rclone4.bat | timeout 21600
   wmic process where name="rclone4.exe" call terminate > nul
   set /a loopCount=%loopCount%-1
   if "%loopCount%"=="0" GOTO :EOF
)

答案1

您必须退出 for 内的管道并且忘记了 /t 开关例如:

调用 c:\rclone\rclone1.bat ^| 超时 /t 21600

  • 我在 Windows 11 上,不放 /t 开关似乎也可以工作,但我不确定其他 Windows 版本是否有效......

您还启动了一个 *.bat 文件,但正在完成一个 *.exe ????

@echo off
set /a maxLoop=999999
set /a loopCount=%maxLoop%
for /l %%x in (1, 1, %maxLoop%) do (
                                    CALL c:\rclone\rclone1.bat
                                    timeout /t 21600 >nul
                                    wmic process where name="rclone1.exe" call terminate > nul
                                    CALL c:\rclone\rclone2.bat
                                    timeout /t 21600 >nul
                                    wmic process where name="rclone2.exe" call terminate > nul 
                                    CALL c:\rclone\rclone3.bat
                                    timeout /t 21600 >nul
                                    wmic process where name="rclone3.exe" call terminate > nul
                                    CALL c:\rclone\rclone4.bat
                                    timeout /t 21600 >nul
                                    wmic process where name="rclone4.exe" call terminate > nul
                                   )

答案2

只需尝试使用管道反转命令:

// replace

command | timeout 

// per

timeout | command

@echo off

set /a maxLoop=999999
set /a loopCount=%maxLoop%

for /l %%x in (1, 1, %maxLoop%) do (
     timeout 21600 | call c:\rclone\rclone1.bat  
     wmic process where name="rclone1.exe" call terminate >nul
     timeout 21600 | call c:\rclone\rclone2.bat
     wmic process where name="rclone2.exe" call terminate >nul
     timeout 21600 | call c:\rclone\rclone3.bat
     wmic process where name="rclone3.exe" call terminate >nul
     timeout 21600 | call c:\rclone\rclone4.bat
     wmic process where name="rclone4.exe" call terminate >nul
     set /a loopCount=%loopCount%-1
     if "%loopCount%"=="0" GOTO :EOF
   )

为了防止被调用的bat执行完成后在控制台中重定向的命令的输出字符串timeout出现在控制台屏幕上,请使用>nul 2>&1

@echo off

set /a maxLoop=999999
set /a loopCount=%maxLoop%

for /l %%x in (1, 1, %maxLoop%) do (
     >nul 2>&1 timeout 21600 | call c:\rclone\rclone1.bat  
     wmic process where name="rclone1.exe" call terminate >nul
     >nul 2>&1 timeout 21600 | call c:\rclone\rclone2.bat
     wmic process where name="rclone2.exe" call terminate >nul
     >nul 2>&1 timeout 21600 | call c:\rclone\rclone3.bat
     wmic process where name="rclone3.exe" call terminate >nul
     >nul 2>&1 timeout 21600 | call c:\rclone\rclone4.bat
     wmic process where name="rclone4.exe" call terminate >nul
     set /a loopCount=%loopCount%-1
     if "%loopCount%"=="0" GOTO :EOF
   )

在平等场景中,如果不使用变量,而是使用我的个人偏好,我会这样做,代码如下:


@echo off

for /l %%i in =;(999999, -000001, 000001)do @for /l %%y in =;(01, 01, 04)do =;(
     >nul 2>&1 timeout 21600 | start /b "" cmd.exe /r "c:\rclone\rclone%%y.bat"
     <con: call wmic process where name="rclone%%y.exe" call terminate >nul );=

如果目的是等待call+timeout命令完成,然后才需要强制rcloneX.exe可执行文件终止,我会尝试使用:

@echo off

for /l %%i in =;(999999, -000001, 000001)do @for /l %%y in =;(01, 01, 04)do =;(
     cmd.exe /v /q /r ">nul 2>&1 timeout.exe 2160 | "c:\rclone\rclone%%~y.bat""
     <con: call wmic process where name="rclone%%y.exe" call terminate >nul );=

相关内容