检查一个特定的bat文件mybatch.bat
是否正在运行可能是比它看起来更加棘手的任务。
寻找在tasklist /V
特定的窗口标题,以及在wmic process where "name='cmd.exe'" get CommandLine
测试CommandLine
财产可能会失败下的一些想象的情况。
1st。你能
- 在
mybatch.bat
和
- 删除所有其他
title
命令开始从mybatch.bat
和
- 添加
title ThisIsDistinguishingString
命令确保mybatch.bat
不包含调用另一个批处理脚本(S)一个title
命令?
然后检查errorlevel
从find
command返回如下:
:testMybatch
tasklist /V /FI "imagename eq cmd.exe" | find "ThisIsDistinguishingString" > nul
if errorlevel 1 (
rem echo mybatch.bat batch not found
) else (
echo mybatch.bat is running %date% %time%
timeout /T 10 /NOBREAK >NUL 2>&1
goto :testMybatch
)
第二。否则,检查是否wmic
Windows Management Instrumentation command输出可以帮助
wmic process where "name='cmd.exe'" get /value
然后,你可以在它的输出缩小到
检测
mybatch.bat
wmic process where "name='cmd.exe'" get CommandLine, ProcessID
注意wmic
可如果一个特定的返回一些Win32_Process
class性能,尤其是CommandLine
,空进程在另一个用户帐户下启动或提升(以管理员身份运行)。
高架wmic
返回全部属性。
'tasklist/v'给出窗口标题。就好像它是窗口标题 - cmd不会改变实际标题。 –