批处理脚本调用可执行文件,并退出,如果它挂起 [英] Batch script to call executable and exit if it hangs

查看:415
本文介绍了批处理脚本调用可执行文件,并退出,如果它挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需要编写一个启动一个可执行文件,并尽快退出脚本作为可执行完成(所以没有IP侦测等待脚本),但会自动杀死.exe和30分钟后退出脚本如果可执行文件是一个批处理脚本仍在运行(挂起,不响应等)

So I need to write a batch script that starts an executable and exits the script as soon as the executable finishes (so no ip pinging wait scripts) but will automatically kill the exe and exit the script after 30 minutes if the executable is still running (hung, not responding, etc)

下面是我迄今。这一发现语句中正确的输出相匹配的进程数,但我的问题是,ERRORLEVEL总是返回0,不管是否有正在运行的匹配可执行文件。

Here is what I have so far. The find statement correctly outputs the number of processes that match, but my problem is that the ERRORLEVEL always returns 0, regardless of whether or not there are matching executables running.

我是相当新的批量脚本,所以它是非常有可能的,我俯瞰一些非常简单的事情。

I am fairly new to batch scripting, so it is very possible I am overlooking some really simple thing.

@echo off
start calc.exe

REM loop 600 times, each loop being 3 seconds (30 minutes total)
FOR /L %%A IN (1,1,600) DO (

   REM find the running executable
   tasklist | find /I /C "calc.exe" > nul
   echo %ERRORLEVEL%

   Rem exit the script if no executable is found (i.e it has run successfully)
   if %ERRORLEVEL% eq 1 EXIT 

   Rem pause for 3 seconds
   ping 1.1.1.1 -n 1 -w 3000 > nul
)

REM kill executable if we haven't exited yet
taskkill /f /im calc.exe

在此先感谢!

推荐答案

延迟扩展

@echo OFF &SETLOCAL ENABLEDELAYEDEXPANSION 
start calc.exe

REM loop 600 times, each loop being 3 seconds (30 minutes total)
FOR /L %%A IN (1,1,600) DO (

   REM find the running executable
   tasklist | find /I /C "calc.exe" > nul
   echo !ERRORLEVEL!

   Rem exit the script if no executable is found (i.e it has run successfully)
   if !ERRORLEVEL! eq 1 EXIT 

   Rem pause for 3 seconds
   ping 1.1.1.1 -n 1 -w 3000 > nul
)

REM kill executable if we haven't exited yet
taskkill /f /im calc.exe

这篇关于批处理脚本调用可执行文件,并退出,如果它挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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