如何检查进程是否通过批处理脚本运行?

时间:2022-10-04 00:12:25

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

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

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.

应用程序也可以作为任何用户运行。

17 个解决方案

#1


274  

Another possibility I came up with, inspired by using grep, is:

我想到的另一种可能是,用grep的灵感,是:

tasklist /FI "IMAGENAME eq myapp.exe" 2>NUL | find /I /N "myapp.exe">NUL
if "%ERRORLEVEL%"=="0" echo Program is running

It doesn't need to save an extra file, so I prefer this method.

它不需要保存一个额外的文件,所以我更喜欢这个方法。

#2


57  

Here's how I've worked it out:

以下是我的研究方法:

tasklist /FI "IMAGENAME eq notepad.exe" /FO CSV > search.log

FOR /F %%A IN (search.log) DO IF %%~zA EQU 0 GOTO end

start notepad.exe

:end

del search.log

The above will open Notepad if it is not already running.

如果它还没有运行,上面将打开记事本。

Edit: Note that this won't find applications hidden from the tasklist. This will include any scheduled tasks running as a different user, as these are automatically hidden.

编辑:注意,这不会发现隐藏在任务列表中的应用程序。这将包括任何作为不同用户运行的调度任务,因为这些任务是自动隐藏的。

#3


32  

I like Chaosmaster's solution! But I looked for a solution which does not start another external program (like find.exe or findstr.exe). So I added the idea from Matt Lacey's solution, which creates an also avoidable temp file. At the end I could find a fairly simple solution, so I share it...

我喜欢Chaosmaster的解决方案!但我寻找的解决方案不启动另一个外部程序(如find)。exe或findstr.exe)。所以我从Matt Lacey的解决方案中添加了这个想法,它创建了一个可以避免的临时文件。最后,我可以找到一个相当简单的解决方案,所以我分享它……

SETLOCAL EnableExtensions
set EXE=myprog.exe
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto FOUND
echo Not running
goto FIN
:FOUND
echo Running
:FIN

This is working for me nicely...

这对我很有效…

#4


20  

Under Windows you can use Windows Management Instrumentation (WMI) to ensure that no apps with the specified command line is launched, for example:

在Windows下,您可以使用Windows管理工具(WMI)来确保没有使用指定命令行的应用程序,例如:

wmic process where (name="nmake.exe") get commandline | findstr /i /c:"/f load.mak" /c:"/f build.mak" > NUL && (echo THE BUILD HAS BEEN STARTED ALREADY! > %ALREADY_STARTED% & exit /b 1)

wmic进程(name="nmake.exe")获取命令行| findstr /i /c:"/f负载。mak / c:“/ f。mak" > NUL && && (echo的构建已经开始!)> %ALREADY_STARTED% & exit / b1)

#5


8  

I use PV.exe from http://www.teamcti.com/pview/prcview.htm installed in Program Files\PV with a batch file like this:

我使用PV。从http://www.teamcti.com/pview/prcview.htm中安装程序文件\PV,其中有这样的批处理文件:

@echo off
PATH=%PATH%;%PROGRAMFILES%\PV;%PROGRAMFILES%\YourProgram
PV.EXE YourProgram.exe >nul
if ERRORLEVEL 1 goto Process_NotFound
:Process_Found
echo YourProgram is running
goto END
:Process_NotFound
echo YourProgram is not running
YourProgram.exe
goto END
:END

#6


8  

The suggestion of npocmaka to use QPROCESS instead of TASKLIST is great but, its answer is so big and complex that I feel obligated to post a quite simplified version of it which, I guess, will solve the problem of most non-advanced users:

npocmaka使用QPROCESS代替TASKLIST的建议很好,但是它的答案是如此的庞大和复杂,以至于我觉得有义务发布一个非常简化的版本,我想这将解决大多数非高级用户的问题:

QPROCESS "myprocess.exe">NUL
IF %ERRORLEVEL% EQU 0 ECHO "Process running"

The code above was tested in Windows 7, with a user with administrator rigths.

上面的代码在Windows 7中进行了测试,用户具有管理员的严格性。

#7


7  

TrueY's answer seemed the most elegant solution, however, I had to do some messing around because I didn't understand what exactly was going on. Let me clear things up to hopefully save some time for the next person.

TrueY的回答似乎是最优雅的解决方案,然而,我不得不做一些混乱的事情,因为我不明白到底发生了什么。我来澄清一下,希望能留点时间给下一个人。

TrueY's modified Answer:

TrueY修改的回答:

::Change the name of notepad.exe to the process .exe that you're trying to track
::Process names are CASE SENSITIVE, so notepad.exe works but Notepad.exe does NOT
::Do not change IMAGENAME
::You can Copy and Paste this into an empty batch file and change the name of
::notepad.exe to the process you'd like to track
::Also, some large programs take a while to no longer show as not running, so
::give this batch a few seconds timer to avoid a false result!!

@echo off
SETLOCAL EnableExtensions

set EXE=notepad.exe

FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto ProcessFound

goto ProcessNotFound

:ProcessFound

echo %EXE% is running
goto END
:ProcessNotFound
echo %EXE% is not running
goto END
:END
echo Finished!

Anyway, I hope that helps. I know sometimes reading batch/command-line can be kind of confusing sometimes if you're kind of a newbie, like me.

不管怎样,我希望这能有所帮助。我知道有时候,如果你像我一样,读批/命令行有时会有点迷惑。

#8


6  

The answer provided by Matt Lacey works for Windows XP. However, in Windows Server 2003 the line

马特·莱西(Matt Lacey)提供的答案适用于Windows XP。然而,在Windows Server 2003中这条线。

 tasklist /FI "IMAGENAME eq notepad.exe" /FO CSV > search.log

returns

返回

INFO: No tasks are running which match the specified criteria.

INFO:没有运行符合指定标准的任务。

which is then read as the process is running.

然后在进程运行时读取该进程。

I don't have a heap of batch scripting experience, so my soulution is to then search for the process name in the search.log file and pump the results into another file and search that for any output.

我没有大量的批处理脚本经验,所以我的方法是在搜索中搜索进程名。日志文件并将结果输入到另一个文件中,并搜索任何输出。

tasklist /FI "IMAGENAME eq notepad.exe" /FO CSV > search.log

FINDSTR notepad.exe search.log > found.log

FOR /F %%A IN (found.log) DO IF %%~zA EQU 0 GOTO end

start notepad.exe

:end

del search.log
del found.log

I hope this helps someone else.

我希望这能帮助别人。

#9


3  

I like the WMIC and TASKLIST tools but they are not available in home/basic editions of windows.Another way is to use QPROCESS command available on almost every windows machine (for the ones that have terminal services - I think only win XP without SP2 , so practialy every windows machine):

我喜欢WMIC和TASKLIST工具,但是它们在home/basic版本的windows中是不可用的。另一种方法是在几乎所有的windows机器上使用QPROCESS命令(对于那些有终端服务的人来说,我认为只有在没有SP2的情况下才会获得XP,所以每个windows机器都是这样操作的):

@echo off
:check_process
setlocal
if "%~1" equ "" echo pass the process name as forst argument && exit /b 1
:: first argument is the process you want to check if running
set process_to_check=%~1
:: QPROCESS can display only the first 12 symbols of the running process
:: If other tool is used the line bellow could be deleted
set process_to_check=%process_to_check:~0,12%

QPROCESS * | find /i "%process_to_check%" >nul 2>&1 && (
    echo process %process_to_check%  is running
) || (
    echo process %process_to_check%  is not running
)
endlocal

QPROCESS command is not so powerful as TASKLIST and is limited in showing only 12 symbols of process name but should be taken into consideration if TASKLIST is not available.

QPROCESS命令并不像TASKLIST那样强大,它只显示了12个进程名称的符号,但是如果任务列表不可用,则应该考虑它。

More simple usage where it uses the name if the process as an argument (the .exe suffix is mandatory in this case where you pass the executable name):

更简单的用法,如果进程作为参数,它使用名称(.exe后缀是强制的,在这种情况下您传递可执行的名称):

@echo off
:check_process
setlocal
if "%~1" equ "" echo pass the process name as forst argument && exit /b 1
:: first argument is the process you want to check if running
:: .exe suffix is mandatory
set "process_to_check=%~1"


QPROCESS "%process_to_check%" >nul 2>&1 && (
    echo process %process_to_check%  is running
) || (
    echo process %process_to_check%  is not running
)
endlocal

The difference between two ways of QPROCESS usage is that the QPROCESS * will list all processes while QPROCESS some.exe will filter only the processes for the current user.

QPROCESS使用的两种方法之间的区别是,QPROCESS *将列出所有进程,而QPROCESS是一些。exe将只过滤当前用户的进程。

Using WMI objects through windows script host exe instead of WMIC is also an option.It should on run also on every windows machine (excluding the ones where the WSH is turned off but this is a rare case).Here bat file that lists all processes through WMI classes and can be used instead of QPROCESS in the script above (it is a jscript/bat hybrid and should be saved as .bat):

使用WMI对象通过windows脚本主机exe而不是WMIC也是一个选项。它也应该在每个windows机器上运行(不包括WSH关闭的那些,但这是一个罕见的情况)。这里的bat文件通过WMI类列出所有进程,可以在上面的脚本中使用而不是QPROCESS(它是一个jscript/bat混合,应该被保存为.bat):

@if (@X)==(@Y) @end /* JSCRIPT COMMENT **


@echo off
cscript //E:JScript //nologo "%~f0"
exit /b

************** end of JSCRIPT COMMENT **/


var winmgmts = GetObject("winmgmts:\\\\.\\root\\cimv2");
var colProcess = winmgmts.ExecQuery("Select * from Win32_Process");
var processes =  new Enumerator(colProcess);
for (;!processes.atEnd();processes.moveNext()) {
    var process=processes.item();
    WScript.Echo( process.processID + "   " + process.Name );
}

And a modification that will check if a process is running:

并进行修改,以检查进程是否正在运行:

@if (@X)==(@Y) @end /* JSCRIPT COMMENT **


@echo off
if "%~1" equ "" echo pass the process name as forst argument && exit /b 1
:: first argument is the process you want to check if running
set process_to_check=%~1

cscript //E:JScript //nologo "%~f0" | find /i "%process_to_check%" >nul 2>&1 && (
    echo process %process_to_check%  is running
) || (
    echo process %process_to_check%  is not running
)

exit /b

************** end of JSCRIPT COMMENT **/


var winmgmts = GetObject("winmgmts:\\\\.\\root\\cimv2");
var colProcess = winmgmts.ExecQuery("Select * from Win32_Process");
var processes =  new Enumerator(colProcess);
for (;!processes.atEnd();processes.moveNext()) {
    var process=processes.item();
    WScript.Echo( process.processID + "   " + process.Name );
}

The two options could be used on machines that have no TASKLIST.

这两个选项可以在没有任务列表的机器上使用。

The ultimate technique is using MSHTA . This will run on every windows machine from XP and above and does not depend on windows script host settings. the call of MSHTA could reduce a little bit the performance though (again should be saved as bat):

最终的技术是使用MSHTA。这将在XP和以上的每个windows机器上运行,并且不依赖于windows脚本主机设置。MSHTA的调用可以减少一些性能,但是(同样应该被保存为bat):

@if (@X)==(@Y) @end /* JSCRIPT COMMENT **
@echo off

setlocal
if "%~1" equ "" echo pass the process name as forst argument && exit /b 1
:: first argument is the process you want to check if running

set process_to_check=%~1


mshta "about:<script language='javascript' src='file://%~dpnxf0'></script>" | find /i "%process_to_check%" >nul 2>&1 && (
    echo process %process_to_check%  is running
) || (
    echo process %process_to_check%  is not running
)
endlocal
exit /b
************** end of JSCRIPT COMMENT **/


   var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);


   var winmgmts = GetObject("winmgmts:\\\\.\\root\\cimv2");
   var colProcess = winmgmts.ExecQuery("Select * from Win32_Process");
   var processes =  new Enumerator(colProcess);
   for (;!processes.atEnd();processes.moveNext()) {
    var process=processes.item();
    fso.Write( process.processID + "   " + process.Name + "\n");
   }
   close();

#10


2  

I don't know how to do so with built in CMD but if you have grep you can try the following:

我不知道怎么用CMD来做,但是如果你有grep,你可以试试下面的方法:

tasklist /FI "IMAGENAME eq myApp.exe" | grep myApp.exe
if ERRORLEVEL 1 echo "myApp is not running"

#11


2  

TASKLIST | FINDSTR ProgramName || START "" "Path\ProgramName.exe"

#12


0  

I'm assuming windows here. So, you'll need to use WMI to get that information. Check out The Scripting Guy's archives for a lot of examples on how to use WMI from a script.

我在这里假设windows。因此,您需要使用WMI来获取这些信息。查看脚本人员的存档,了解如何从脚本中使用WMI的许多示例。

#13


0  

I used the script provided by Matt (2008-10-02). The only thing I had trouble with was that it wouldn't delete the search.log file. I expect because I had to cd to another location to start my program. I cd'd back to where the BAT file and search.log are, but it still wouldn't delete. So I resolved that by deleting the search.log file first instead of last.

我使用了Matt提供的脚本(2008-10-02)。我唯一的麻烦是它不会删除搜索。日志文件。我想,因为我必须cd到另一个地方开始我的程序。我回到了蝙蝠档案和搜索的地方。日志是,但它仍然不会删除。所以我通过删除搜索来解决这个问题。将日志文件放在首位而不是最后。

del search.log

tasklist /FI "IMAGENAME eq myprog.exe" /FO CSV > search.log

FOR /F %%A IN (search.log) DO IF %%-zA EQU 0 GOTO end

cd "C:\Program Files\MyLoc\bin"

myprog.exe myuser mypwd

:end

#14


0  

You should check the parent process name, see The Code Project article about a .NET based solution**.

您应该检查父进程名称,查看关于基于. net的解决方案**的代码项目文章。

A non-programmatic way to check:

一种非程序性的检查方法:

  1. Launch Cmd.exe
  2. 发射用于cmd . exe
  3. Launch an application (for instance, c:\windows\notepad.exe)
  4. 启动应用程序(例如,c:\ window\ notepad.exe)
  5. Check properties of the Notepad.exe process in Process Explorer
  6. 检查记事本的属性。进程管理器中的exe进程。
  7. Check for parent process (This shows cmd.exe)
  8. 检查父进程(这显示了cmd.exe)

The same can be checked by getting the parent process name.

可以通过获取父进程名称来检查相同的内容。

#15


0  

I usually execute following command in cmd prompt to check if my program.exe is running or not:

我通常在cmd提示符下执行以下命令来检查我的程序。exe是否运行:

tasklist | grep program

#16


0  

Just mentioning, if your task name is really long then it won't appear in its entirety in the tasklist result, so it might be safer (other than localization) to check for the opposite.

只要提到,如果您的任务名称真的很长,那么它就不会在任务列表结果中完整地显示出来,所以它可能更安全(而不是本地化)来检查相反的结果。

Variation of this answer:

变异的回答:

:: in case your task name is really long, check for the 'opposite' and find  the message when it's not there
tasklist /fi "imagename eq yourreallylongtasknamethatwontfitinthelist.exe" 2>NUL | find /I /N "no tasks are running">NUL
if "%errorlevel%"=="0" (
    echo Task Found
) else (
    echo Not Found Task
)

#17


-1  

Building on vtrz's answer and Samuel Renkert's answer on an other topic, I came up with the following script that only runs %EXEC_CMD% if it isn't already running:

在vtrz的回答和塞缪尔·伦克特关于另一个主题的回答中,我想到了下面的脚本,它只运行%EXEC_CMD%,如果它还没有运行的话:

@echo off
set EXEC_CMD="rsync.exe"
wmic process where (name=%EXEC_CMD%) get commandline | findstr /i %EXEC_CMD%> NUL
if errorlevel 1 (
    %EXEC_CMD% ...
) else (
    @echo not starting %EXEC_CMD%: already running.
)

As was said before, this requires administrative privileges.

如前所述,这需要管理特权。

#1


274  

Another possibility I came up with, inspired by using grep, is:

我想到的另一种可能是,用grep的灵感,是:

tasklist /FI "IMAGENAME eq myapp.exe" 2>NUL | find /I /N "myapp.exe">NUL
if "%ERRORLEVEL%"=="0" echo Program is running

It doesn't need to save an extra file, so I prefer this method.

它不需要保存一个额外的文件,所以我更喜欢这个方法。

#2


57  

Here's how I've worked it out:

以下是我的研究方法:

tasklist /FI "IMAGENAME eq notepad.exe" /FO CSV > search.log

FOR /F %%A IN (search.log) DO IF %%~zA EQU 0 GOTO end

start notepad.exe

:end

del search.log

The above will open Notepad if it is not already running.

如果它还没有运行,上面将打开记事本。

Edit: Note that this won't find applications hidden from the tasklist. This will include any scheduled tasks running as a different user, as these are automatically hidden.

编辑:注意,这不会发现隐藏在任务列表中的应用程序。这将包括任何作为不同用户运行的调度任务,因为这些任务是自动隐藏的。

#3


32  

I like Chaosmaster's solution! But I looked for a solution which does not start another external program (like find.exe or findstr.exe). So I added the idea from Matt Lacey's solution, which creates an also avoidable temp file. At the end I could find a fairly simple solution, so I share it...

我喜欢Chaosmaster的解决方案!但我寻找的解决方案不启动另一个外部程序(如find)。exe或findstr.exe)。所以我从Matt Lacey的解决方案中添加了这个想法,它创建了一个可以避免的临时文件。最后,我可以找到一个相当简单的解决方案,所以我分享它……

SETLOCAL EnableExtensions
set EXE=myprog.exe
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto FOUND
echo Not running
goto FIN
:FOUND
echo Running
:FIN

This is working for me nicely...

这对我很有效…

#4


20  

Under Windows you can use Windows Management Instrumentation (WMI) to ensure that no apps with the specified command line is launched, for example:

在Windows下,您可以使用Windows管理工具(WMI)来确保没有使用指定命令行的应用程序,例如:

wmic process where (name="nmake.exe") get commandline | findstr /i /c:"/f load.mak" /c:"/f build.mak" > NUL && (echo THE BUILD HAS BEEN STARTED ALREADY! > %ALREADY_STARTED% & exit /b 1)

wmic进程(name="nmake.exe")获取命令行| findstr /i /c:"/f负载。mak / c:“/ f。mak" > NUL && && (echo的构建已经开始!)> %ALREADY_STARTED% & exit / b1)

#5


8  

I use PV.exe from http://www.teamcti.com/pview/prcview.htm installed in Program Files\PV with a batch file like this:

我使用PV。从http://www.teamcti.com/pview/prcview.htm中安装程序文件\PV,其中有这样的批处理文件:

@echo off
PATH=%PATH%;%PROGRAMFILES%\PV;%PROGRAMFILES%\YourProgram
PV.EXE YourProgram.exe >nul
if ERRORLEVEL 1 goto Process_NotFound
:Process_Found
echo YourProgram is running
goto END
:Process_NotFound
echo YourProgram is not running
YourProgram.exe
goto END
:END

#6


8  

The suggestion of npocmaka to use QPROCESS instead of TASKLIST is great but, its answer is so big and complex that I feel obligated to post a quite simplified version of it which, I guess, will solve the problem of most non-advanced users:

npocmaka使用QPROCESS代替TASKLIST的建议很好,但是它的答案是如此的庞大和复杂,以至于我觉得有义务发布一个非常简化的版本,我想这将解决大多数非高级用户的问题:

QPROCESS "myprocess.exe">NUL
IF %ERRORLEVEL% EQU 0 ECHO "Process running"

The code above was tested in Windows 7, with a user with administrator rigths.

上面的代码在Windows 7中进行了测试,用户具有管理员的严格性。

#7


7  

TrueY's answer seemed the most elegant solution, however, I had to do some messing around because I didn't understand what exactly was going on. Let me clear things up to hopefully save some time for the next person.

TrueY的回答似乎是最优雅的解决方案,然而,我不得不做一些混乱的事情,因为我不明白到底发生了什么。我来澄清一下,希望能留点时间给下一个人。

TrueY's modified Answer:

TrueY修改的回答:

::Change the name of notepad.exe to the process .exe that you're trying to track
::Process names are CASE SENSITIVE, so notepad.exe works but Notepad.exe does NOT
::Do not change IMAGENAME
::You can Copy and Paste this into an empty batch file and change the name of
::notepad.exe to the process you'd like to track
::Also, some large programs take a while to no longer show as not running, so
::give this batch a few seconds timer to avoid a false result!!

@echo off
SETLOCAL EnableExtensions

set EXE=notepad.exe

FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto ProcessFound

goto ProcessNotFound

:ProcessFound

echo %EXE% is running
goto END
:ProcessNotFound
echo %EXE% is not running
goto END
:END
echo Finished!

Anyway, I hope that helps. I know sometimes reading batch/command-line can be kind of confusing sometimes if you're kind of a newbie, like me.

不管怎样,我希望这能有所帮助。我知道有时候,如果你像我一样,读批/命令行有时会有点迷惑。

#8


6  

The answer provided by Matt Lacey works for Windows XP. However, in Windows Server 2003 the line

马特·莱西(Matt Lacey)提供的答案适用于Windows XP。然而,在Windows Server 2003中这条线。

 tasklist /FI "IMAGENAME eq notepad.exe" /FO CSV > search.log

returns

返回

INFO: No tasks are running which match the specified criteria.

INFO:没有运行符合指定标准的任务。

which is then read as the process is running.

然后在进程运行时读取该进程。

I don't have a heap of batch scripting experience, so my soulution is to then search for the process name in the search.log file and pump the results into another file and search that for any output.

我没有大量的批处理脚本经验,所以我的方法是在搜索中搜索进程名。日志文件并将结果输入到另一个文件中,并搜索任何输出。

tasklist /FI "IMAGENAME eq notepad.exe" /FO CSV > search.log

FINDSTR notepad.exe search.log > found.log

FOR /F %%A IN (found.log) DO IF %%~zA EQU 0 GOTO end

start notepad.exe

:end

del search.log
del found.log

I hope this helps someone else.

我希望这能帮助别人。

#9


3  

I like the WMIC and TASKLIST tools but they are not available in home/basic editions of windows.Another way is to use QPROCESS command available on almost every windows machine (for the ones that have terminal services - I think only win XP without SP2 , so practialy every windows machine):

我喜欢WMIC和TASKLIST工具,但是它们在home/basic版本的windows中是不可用的。另一种方法是在几乎所有的windows机器上使用QPROCESS命令(对于那些有终端服务的人来说,我认为只有在没有SP2的情况下才会获得XP,所以每个windows机器都是这样操作的):

@echo off
:check_process
setlocal
if "%~1" equ "" echo pass the process name as forst argument && exit /b 1
:: first argument is the process you want to check if running
set process_to_check=%~1
:: QPROCESS can display only the first 12 symbols of the running process
:: If other tool is used the line bellow could be deleted
set process_to_check=%process_to_check:~0,12%

QPROCESS * | find /i "%process_to_check%" >nul 2>&1 && (
    echo process %process_to_check%  is running
) || (
    echo process %process_to_check%  is not running
)
endlocal

QPROCESS command is not so powerful as TASKLIST and is limited in showing only 12 symbols of process name but should be taken into consideration if TASKLIST is not available.

QPROCESS命令并不像TASKLIST那样强大,它只显示了12个进程名称的符号,但是如果任务列表不可用,则应该考虑它。

More simple usage where it uses the name if the process as an argument (the .exe suffix is mandatory in this case where you pass the executable name):

更简单的用法,如果进程作为参数,它使用名称(.exe后缀是强制的,在这种情况下您传递可执行的名称):

@echo off
:check_process
setlocal
if "%~1" equ "" echo pass the process name as forst argument && exit /b 1
:: first argument is the process you want to check if running
:: .exe suffix is mandatory
set "process_to_check=%~1"


QPROCESS "%process_to_check%" >nul 2>&1 && (
    echo process %process_to_check%  is running
) || (
    echo process %process_to_check%  is not running
)
endlocal

The difference between two ways of QPROCESS usage is that the QPROCESS * will list all processes while QPROCESS some.exe will filter only the processes for the current user.

QPROCESS使用的两种方法之间的区别是,QPROCESS *将列出所有进程,而QPROCESS是一些。exe将只过滤当前用户的进程。

Using WMI objects through windows script host exe instead of WMIC is also an option.It should on run also on every windows machine (excluding the ones where the WSH is turned off but this is a rare case).Here bat file that lists all processes through WMI classes and can be used instead of QPROCESS in the script above (it is a jscript/bat hybrid and should be saved as .bat):

使用WMI对象通过windows脚本主机exe而不是WMIC也是一个选项。它也应该在每个windows机器上运行(不包括WSH关闭的那些,但这是一个罕见的情况)。这里的bat文件通过WMI类列出所有进程,可以在上面的脚本中使用而不是QPROCESS(它是一个jscript/bat混合,应该被保存为.bat):

@if (@X)==(@Y) @end /* JSCRIPT COMMENT **


@echo off
cscript //E:JScript //nologo "%~f0"
exit /b

************** end of JSCRIPT COMMENT **/


var winmgmts = GetObject("winmgmts:\\\\.\\root\\cimv2");
var colProcess = winmgmts.ExecQuery("Select * from Win32_Process");
var processes =  new Enumerator(colProcess);
for (;!processes.atEnd();processes.moveNext()) {
    var process=processes.item();
    WScript.Echo( process.processID + "   " + process.Name );
}

And a modification that will check if a process is running:

并进行修改,以检查进程是否正在运行:

@if (@X)==(@Y) @end /* JSCRIPT COMMENT **


@echo off
if "%~1" equ "" echo pass the process name as forst argument && exit /b 1
:: first argument is the process you want to check if running
set process_to_check=%~1

cscript //E:JScript //nologo "%~f0" | find /i "%process_to_check%" >nul 2>&1 && (
    echo process %process_to_check%  is running
) || (
    echo process %process_to_check%  is not running
)

exit /b

************** end of JSCRIPT COMMENT **/


var winmgmts = GetObject("winmgmts:\\\\.\\root\\cimv2");
var colProcess = winmgmts.ExecQuery("Select * from Win32_Process");
var processes =  new Enumerator(colProcess);
for (;!processes.atEnd();processes.moveNext()) {
    var process=processes.item();
    WScript.Echo( process.processID + "   " + process.Name );
}

The two options could be used on machines that have no TASKLIST.

这两个选项可以在没有任务列表的机器上使用。

The ultimate technique is using MSHTA . This will run on every windows machine from XP and above and does not depend on windows script host settings. the call of MSHTA could reduce a little bit the performance though (again should be saved as bat):

最终的技术是使用MSHTA。这将在XP和以上的每个windows机器上运行,并且不依赖于windows脚本主机设置。MSHTA的调用可以减少一些性能,但是(同样应该被保存为bat):

@if (@X)==(@Y) @end /* JSCRIPT COMMENT **
@echo off

setlocal
if "%~1" equ "" echo pass the process name as forst argument && exit /b 1
:: first argument is the process you want to check if running

set process_to_check=%~1


mshta "about:<script language='javascript' src='file://%~dpnxf0'></script>" | find /i "%process_to_check%" >nul 2>&1 && (
    echo process %process_to_check%  is running
) || (
    echo process %process_to_check%  is not running
)
endlocal
exit /b
************** end of JSCRIPT COMMENT **/


   var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);


   var winmgmts = GetObject("winmgmts:\\\\.\\root\\cimv2");
   var colProcess = winmgmts.ExecQuery("Select * from Win32_Process");
   var processes =  new Enumerator(colProcess);
   for (;!processes.atEnd();processes.moveNext()) {
    var process=processes.item();
    fso.Write( process.processID + "   " + process.Name + "\n");
   }
   close();

#10


2  

I don't know how to do so with built in CMD but if you have grep you can try the following:

我不知道怎么用CMD来做,但是如果你有grep,你可以试试下面的方法:

tasklist /FI "IMAGENAME eq myApp.exe" | grep myApp.exe
if ERRORLEVEL 1 echo "myApp is not running"

#11


2  

TASKLIST | FINDSTR ProgramName || START "" "Path\ProgramName.exe"

#12


0  

I'm assuming windows here. So, you'll need to use WMI to get that information. Check out The Scripting Guy's archives for a lot of examples on how to use WMI from a script.

我在这里假设windows。因此,您需要使用WMI来获取这些信息。查看脚本人员的存档,了解如何从脚本中使用WMI的许多示例。

#13


0  

I used the script provided by Matt (2008-10-02). The only thing I had trouble with was that it wouldn't delete the search.log file. I expect because I had to cd to another location to start my program. I cd'd back to where the BAT file and search.log are, but it still wouldn't delete. So I resolved that by deleting the search.log file first instead of last.

我使用了Matt提供的脚本(2008-10-02)。我唯一的麻烦是它不会删除搜索。日志文件。我想,因为我必须cd到另一个地方开始我的程序。我回到了蝙蝠档案和搜索的地方。日志是,但它仍然不会删除。所以我通过删除搜索来解决这个问题。将日志文件放在首位而不是最后。

del search.log

tasklist /FI "IMAGENAME eq myprog.exe" /FO CSV > search.log

FOR /F %%A IN (search.log) DO IF %%-zA EQU 0 GOTO end

cd "C:\Program Files\MyLoc\bin"

myprog.exe myuser mypwd

:end

#14


0  

You should check the parent process name, see The Code Project article about a .NET based solution**.

您应该检查父进程名称,查看关于基于. net的解决方案**的代码项目文章。

A non-programmatic way to check:

一种非程序性的检查方法:

  1. Launch Cmd.exe
  2. 发射用于cmd . exe
  3. Launch an application (for instance, c:\windows\notepad.exe)
  4. 启动应用程序(例如,c:\ window\ notepad.exe)
  5. Check properties of the Notepad.exe process in Process Explorer
  6. 检查记事本的属性。进程管理器中的exe进程。
  7. Check for parent process (This shows cmd.exe)
  8. 检查父进程(这显示了cmd.exe)

The same can be checked by getting the parent process name.

可以通过获取父进程名称来检查相同的内容。

#15


0  

I usually execute following command in cmd prompt to check if my program.exe is running or not:

我通常在cmd提示符下执行以下命令来检查我的程序。exe是否运行:

tasklist | grep program

#16


0  

Just mentioning, if your task name is really long then it won't appear in its entirety in the tasklist result, so it might be safer (other than localization) to check for the opposite.

只要提到,如果您的任务名称真的很长,那么它就不会在任务列表结果中完整地显示出来,所以它可能更安全(而不是本地化)来检查相反的结果。

Variation of this answer:

变异的回答:

:: in case your task name is really long, check for the 'opposite' and find  the message when it's not there
tasklist /fi "imagename eq yourreallylongtasknamethatwontfitinthelist.exe" 2>NUL | find /I /N "no tasks are running">NUL
if "%errorlevel%"=="0" (
    echo Task Found
) else (
    echo Not Found Task
)

#17


-1  

Building on vtrz's answer and Samuel Renkert's answer on an other topic, I came up with the following script that only runs %EXEC_CMD% if it isn't already running:

在vtrz的回答和塞缪尔·伦克特关于另一个主题的回答中,我想到了下面的脚本,它只运行%EXEC_CMD%,如果它还没有运行的话:

@echo off
set EXEC_CMD="rsync.exe"
wmic process where (name=%EXEC_CMD%) get commandline | findstr /i %EXEC_CMD%> NUL
if errorlevel 1 (
    %EXEC_CMD% ...
) else (
    @echo not starting %EXEC_CMD%: already running.
)

As was said before, this requires administrative privileges.

如前所述,这需要管理特权。