Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.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_Command_Cmd - Fatal编程技术网

Windows 批处理程序用于检查进程是否存在

Windows 批处理程序用于检查进程是否存在,windows,batch-file,command,cmd,Windows,Batch File,Command,Cmd,我想要一个批处理程序,它将检查进程notepad.exe是否存在 如果存在notepad.exe,它将结束进程 否则批处理程序将自行关闭 以下是我所做的: @echo off tasklist /fi "imagename eq notepad.exe" > nul if errorlevel 1 taskkill /f /im "notepad.exe" exit 但它不起作用。我的代码有什么问题?任务列表未设置错误级别 echo off tasklist /fi "imagename

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

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

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

以下是我所做的:

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

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

任务列表未设置错误级别

echo off
tasklist /fi "imagename eq notepad.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"
exit
应该执行此任务,因为只有在未找到任务时,“:”才会出现在
任务列表
输出中,因此
FIND
未找到
的错误级别设置为
0
,而
找到
的错误级别设置为
1

然而,

taskkill/f/im“notepad.exe”

如果存在记事本任务,它将杀死它-如果不存在记事本任务,它将无法执行任何操作,因此您实际上不需要进行测试-除非您想做其他事情…比如

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

这似乎是按照您的要求执行的-如果记事本进程存在,请将其杀死,然后退出-否则继续批处理

任务列表
不会设置可在批处理文件中签入的退出代码。检查退出代码的一个解决方法是解析其标准输出(您目前正在重定向到
NUL
)。显然,如果找到该进程,
TASKLIST
将显示其详细信息,其中也包括图像名称。因此,您可以使用
FIND
FINDSTR
检查
任务列表的输出是否包含您在请求中指定的名称。如果搜索失败,则
FIND
FINDSTR
都会设置非空的退出代码。因此,这将起作用:

@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
试试这个:

@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

这是一个单行解决方案

只有当进程确实在运行时,它才会运行taskkill,否则它只会通知它没有运行

tasklist | find /i "notepad.exe" && taskkill /im notepad.exe /F || echo process "notepad.exe" not running.
这是进程正在运行时的输出:

notepad.exe           1960 Console                   0    112,260 K
SUCCESS: The process "notepad.exe" with PID 1960 has been terminated.
这是未运行时的输出:

process "notepad.exe" not running.

这就是它不工作的原因,因为您编写了一些不正确的代码,这就是为什么它总是退出,脚本执行器会将其读取为不可操作的批处理文件,从而阻止它退出并停止 一定是这样

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
)
而不是

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

为什么不在不验证任务是否存在的情况下调用
taskkill
taskkill
不会为我更改错误级别…@Andriy M我想检查进程是否存在。然后结束该过程或自行关闭。无论是否找到并删除图像,脚本似乎都会自行关闭。如果错误级别为
,则可能会重复而不是检查
,这可以进一步简化。也许最好
查找“notepad.exe”
由于输出是翻译的,我们无法确定是否以所有语言显示
。我发现如果图像名称(包括文件扩展名)超过25个字符,则任务列表输出中的“图像名称”列将被截断,这使得具有图像名称的“查找”命令无法工作。但是,只通过“:”字符进行检测似乎太松散,我担心这可能会发生在未来版本的正向输出中。还有其他更好的方法吗?@Elliott:如果您将可执行文件的名称存储在一个变量中,那么您可以在
tasklist
中使用整个值,并且只使用
find
中的前25个字符:
tasklist/fi“imagename eq%imgname%”中的前25个字符。它在Windows 10中工作,我在几个脚本中使用了它。也许我错过了一些东西。如果进程没有运行,errorlevel仍然是0,这也很好。
@echo off
tasklist /fi "imagename eq notepad.exe" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"
exit