Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 在批处理文件中,如何判断进程是否正在运行?_Windows_Batch File_Process - Fatal编程技术网

Windows 在批处理文件中,如何判断进程是否正在运行?

Windows 在批处理文件中,如何判断进程是否正在运行?,windows,batch-file,process,Windows,Batch File,Process,我想编写一个批处理文件,检查进程是否正在运行,如果正在运行,则执行一个操作,如果没有,则执行另一个操作 我知道我可以使用tasklist列出所有正在运行的进程,但有没有更简单的方法直接检查特定进程 看起来这应该行得通,但是不行: tasklist /fi "imagename eq firefox.exe" /hn | MyTask IF %MyTask%=="" GOTO DO_NOTHING 'do something here :DO_NOTHING 使用atzz提供的解决方案,这里是一

我想编写一个批处理文件,检查进程是否正在运行,如果正在运行,则执行一个操作,如果没有,则执行另一个操作

我知道我可以使用tasklist列出所有正在运行的进程,但有没有更简单的方法直接检查特定进程

看起来这应该行得通,但是不行:

tasklist /fi "imagename eq firefox.exe" /hn | MyTask
IF %MyTask%=="" GOTO DO_NOTHING
'do something here
:DO_NOTHING
使用atzz提供的解决方案,这里是一个完整的工作演示:

编辑:经过简化和修改,可在WinXP和Vista下工作

echo off

set process_1="firefox.exe"
set process_2="iexplore.exe"
set ignore_result=INFO:

for /f "usebackq" %%A in (`tasklist /nh /fi "imagename eq %process_1%"`) do if not %%A==%ignore_result% Exit
for /f "usebackq" %%B in (`tasklist /nh /fi "imagename eq %process_2%"`) do if not %%B==%ignore_result% Exit

start "C:\Program Files\Internet Explorer\iexplore.exe" www.google.com
一些选择:

  • 来自微软的PsList

  • Cygwin(私人秘书)
  • 资源工具包(用于ps.vbs)

  • 获取进程的Powershell
您可以使用“for/f”构造来分析程序输出

set running=0
for /f "usebackq" %%T in (`tasklist /nh /fi "imagename eq firefox.exe"`) do set running=1
另外,坚持一个好主意

setlocal EnableExtensions
在脚本开始时,以防用户在默认情况下禁用它