里面一个批处理文件,我怎么能知道一个进程是否正在运行? [英] Inside a batch file, how can I tell whether a process is running?

查看:149
本文介绍了里面一个批处理文件,我怎么能知道一个进程是否正在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写出检查,看是否有进程正在运行,并采取一个动作,如果它是一个批处理文件,另一个动作,如果事实并非如此。

我知道我可以使用任务列表列出所有正在运行的进程,但有一个简单的方法,在特定的方法直接检查?

看起来这应该工作,但它并不:

 任务列表/网络连接imagename EQ firefox.exe/ HN | MyTask
IF%MyTask%==GOTO DO_NOTHING
在这里做一些事情
:没做什么

利用atzz提供的解决方案,这里是一个完整的工作演示:

编辑:简化,和修改,以双方的WinXP和Vista下正常工作

  ECHO OFF设置process_1 =firefox.exe
设置process_2 =IEXPLORE.EXE
设置ignore_result = INFO:FOR / F有usebackq%%的(`任务列表/ NH /网络连接imagename EQ%process_1%`)也若不%% A ==%ignore_result%A出口
FOR / F有usebackq%%的(`任务列表/ NH /网络连接imagename EQ%process_2%`)也若不%%乙==%ignore_result%B出口启动C:\\ Program Files文件\\的Internet Explorer \\ IEXPLORE.EXEwww.google.com


解决方案

您可以使用FOR / F结构分析程序的输出。

 设置运行= 0
FOR / F有usebackq%%吨(`任务列表/ NH /网络连接imagename EQ firefox.exe`)设置不运行= 1

此外,这是一个好主意,坚持一个

  SETLOCAL ENABLEEXTENSIONS

在你的脚本的begginning,以防万一,如果用户有它默认为禁用。

I'd like to write a batch file that checks to see if a process is running, and takes one action if it is, and another action if it isn't.

I know I can use tasklist to list all running processes, but is there a simpler way to directly check on a specific process?

It seems like this should work, but it doesn't:

tasklist /fi "imagename eq firefox.exe" /hn | MyTask
IF %MyTask%=="" GOTO DO_NOTHING
'do something here
:DO_NOTHING

Using the solution provided by atzz, here is a complete working demo:

Edit: Simplified, and modified to work under both WinXP and 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

解决方案

You can use "for /f" construct to analyze program output.

set running=0
for /f "usebackq" %%T in (`tasklist /nh /fi "imagename eq firefox.exe"`) do set running=1

Also, it's a good idea to stick a

setlocal EnableExtensions

at the begginning of your script, just in case if the user has it disabled by default.

这篇关于里面一个批处理文件,我怎么能知道一个进程是否正在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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