如何通过批处理脚本检查进程是否正在运行 [英] How to check if a process is running via a batch script

查看:122
本文介绍了如何通过批处理脚本检查进程是否正在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查应用程序是否正在从批处理(以及 cmd)文件中运行?

How can I check if an application is running from a batch (well cmd) file?

如果程序已经在运行,我不需要启动另一个实例.(我无法更改应用以使其仅成为单个实例.)

I need to not launch another instance if a program is already running. (I can't change the app to make it single instance only.)

该应用程序也可以以任何用户身份运行.

Also the application could be running as any user.

推荐答案

我想到的另一种可能性,它不需要保存文件,灵感来自于 grep 是:

Another possibility I came up with, which does not require to save a file, inspired by using grep is:

tasklist /fi "ImageName eq MyApp.exe" /fo csv 2>NUL | find /I "myapp.exe">NUL
if "%ERRORLEVEL%"=="0" echo Program is running

  • /fi "" 定义要查找的应用程序过滤器,在我们的例子中是 *.exe 名称
  • /fo csv 定义输出格式,csv 是必需的,因为默认情况下,如果可执行文件的名称太长,则可能会被截断,因此 find<将无法匹配/code> 稍后.
  • find/I 表示不区分大小写匹配,可以省略
    • /fi "" defines a filter of apps to find, in our case it's the *.exe name
    • /fo csv defines the output format, csv is required because by default the name of the executable may be truncated if it is too long and thus wouldn't be matched by find later.
    • find /I means case-insensitive matching and may be omitted
    • 请参阅tasklist 命令用于整个语法.

      See the man page of the tasklist command for the whole syntax.

      这篇关于如何通过批处理脚本检查进程是否正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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