windows 批处理程序以检查进程是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15449034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:21:38  来源:igfitidea点击:

Batch program to to check if process exists

windowsbatch-filecommandcmd

提问by user2176930

I want a batch program, which will check if the process notepad.exeexists.

我想要一个批处理程序,它将检查进程是否notepad.exe存在。

ifnotepad.exeexists, it will end the process,

如果notepad.exe存在,它将结束进程,

elsethe batch program will close itself.

否则批处理程序将自行关闭。

Here is what I've done:

这是我所做的:

@echo off
tasklist /fi "imagename eq notepad.exe" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"
exit

But it doesn't work. What is the wrong in my code?

但它不起作用。我的代码有什么问题?

回答by Magoo

TASKLISTdoes not set errorlevel.

TASKLIST不设置错误级别。

echo off
tasklist /fi "imagename eq notepad.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"
exit

should do the job, since ":" should appear in TASKLISToutput only if the task is NOT found, hence FINDwill set the errorlevel to 0for not foundand 1for found

应该完成这项工作,因为“:”应该TASKLIST仅在未找到任务时出现在输出中,因此FIND会将错误级别设置为0fornot found1forfound

Nevertheless,

尽管如此,

taskkill /f /im "notepad.exe"

taskkill /f /im "notepad.exe"

will kill a notepad task if it exists - it can do nothing if no notepad task exists, so you don't really need to test - unless there's something else you want to do...like perhaps

如果记事本任务存在,它将杀死记事本任务 - 如果不存在记事本任务,它什么也不做,所以你真的不需要测试 - 除非你想做其他事情......

echo off
tasklist /fi "imagename eq notepad.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"&exit

which would appear to do as you ask - kill the notepad process if it exists, then exit - otherwise continue with the batch

这似乎按照您的要求进行 - 如果存在,则终止记事本进程,然后退出 - 否则继续批处理

回答by nicovota

This is a one line solution.

这是一种单行解决方案

It will run taskkill only if the process is really running otherwise it will just info that it is not running.

只有当进程真正运行时它才会运行 taskkill 否则它只会显示它没有运行。

tasklist | find /i "notepad.exe" && taskkill /im notepad.exe /F || echo process "notepad.exe" not running.

This is the output in case the process was running:

这是进程运行时的输出:

notepad.exe           1960 Console                   0    112,260 K
SUCCESS: The process "notepad.exe" with PID 1960 has been terminated.

This is the output in case not running:

这是不运行时的输出:

process "notepad.exe" not running.

回答by Andriy M

TASKLISTdoesn't set an exit code that you could check in a batch file. One workaround to checking the exit code could be parsing its standard output (which you are presently redirecting to NUL). Apparently, if the process is found, TASKLISTwill display its details, which include the image name too. Therefore, you could just use FINDor FINDSTRto check if the TASKLIST's output contains the name you have specified in the request. Both FINDand FINDSTRset a non-null exit code if the search was unsuccessful. So, this would work:

TASKLIST没有设置可以在批处理文件中检入的退出代码。检查退出代码的一种解决方法可能是解析其标准输出(您目前正在重定向到NUL)。显然,如果找到该进程,TASKLIST将显示其详细信息,其中也包括图像名称。因此,您可以只使用FINDFINDSTR检查TASKLIST的输出是否包含您在请求中指定的名称。双方FINDFINDSTR设置非空退出代码,如果搜索没有成功。所以,这会起作用:

@echo off
tasklist /fi "imagename eq notepad.exe" | find /i "notepad.exe" > nul
if not errorlevel 1 (taskkill /f /im "notepad.exe") else (
  specific commands to perform if the process was not found
)
exit

There's also an alternative that doesn't involve TASKLISTat all. Unlike TASKLIST, TASKKILLdoes set an exit code. In particular, if it couldn't terminate a process because it simply didn't exist, it would set the exit code of 128. You could check for that code to perform your specific actions that you might need to perform in case the specified process didn't exist:

还有一个完全不涉及的替代方案TASKLIST。与 不同TASKLISTTASKKILL确实设置了退出代码。特别是,如果它无法终止一个进程,因为它根本不存在,它会将退出代码设置为 128。您可以检查该代码以执行您可能需要执行的特定操作,以防指定的进程不存在:

@echo off
taskkill /f /im "notepad.exe" > nul
if errorlevel 128 (
  specific commands to perform if the process
  was not terminated because it was not found
)
exit

回答by Ultra Gamer Ph

That's why it's not working because you code something that is not right, that's why it always exit and the script executer will read it as not operable batch file that prevent it to exit and stop so it must be

这就是它不起作用的原因,因为您编写了一些不正确的代码,这就是为什么它总是退出并且脚本执行器会将其读取为不可操作的批处理文件,以防止它退出和停止,因此它必须是

tasklist /fi "IMAGENAME eq Notepad.exe" 2>NUL | find /I /N "Notepad.exe">NUL
if "%ERRORLEVEL%"=="0" (
msg * Program is running
goto Exit
)
else if "%ERRORLEVEL%"=="1" (
msg * Program is not running
goto Exit
)

rather than

而不是

@echo off
tasklist /fi "imagename eq notepad.exe" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"
exit

回答by Cyber

Try this:

尝试这个:

@echo off
set run=
tasklist /fi "imagename eq notepad.exe" | find ":" > nul
if errorlevel 1 set run=yes
if "%run%"=="yes" echo notepad is running
if "%run%"=="" echo notepad is not running
pause