Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

windows - Why is my usage of command TASKLIST not working as expected?

I have a problem on checking the running state of another batch file using the command TASKLIST and continue processing the batch file depending on the result.

This is the code of my batch file which should check the running state of the other batch file:

@echo off
tasklist /FI "WINDOWTITLE eq C:Windowssystem32cmd.exe - C:
utaejecucion_prueba.bat" /FI "STATUS eq running"
if eq = running "not happening"
if ne = running "start  C:
utaejecucion_prueba.bat"
exit

This code does not work as expected. The output on execution is:

INFO: No tasks are running which match the specified criteria.
= was unexpected at this time.

What is wrong and how to do the batch file execution check correct?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

tasklist.exe does not write to stdErr or record an ErrorLevel you can use, to determine whether the filters returned a task. In order to determine that, you need to use find.exe or findstr.exe to check for a known character or string in a successful output. You can then use the returned ErrorLevel or Success/Failure of that to validate instead.

The only 'relatively robust' way to perform this task using tasklist.exe is to first ensure that you initially ran your batch file, C: utaejecucion_prueba.bat using the following command:

Start "?" C:
utaejecucion_prueba.bat

Or (recommended):

Start "?" "C:
utaejecucion_prueba.bat"

With that done, you could run your verification batch file, with the following line in its content:

%SystemRoot%System32asklist.exe /Fi "ImageName Eq cmd.exe" /Fi "Status Eq Running" /Fi "WindowTitle Eq ? - C:
utaejecucion_prueba.bat" | %SystemRoot%System32find.exe "=" 1> NUL || Start "?" "C:
utaejecucion_prueba.bat"

However, if your batch file path contains spaces:

C:
utaejecucion prueba.bat

You'd need to have initially ran it using:

Start "?" "C:
utaejecucion prueba.bat"

Then change the command in your batch script to:

%SystemRoot%System32asklist.exe /Fi "ImageName Eq cmd.exe" /Fi "Status Eq Running" /Fi "WindowTitle Eq ? - "C:
utaejecucion prueba.bat"" | %SystemRoot%System32find.exe "=" 1> NUL || Start "?" "C:
utaejecucion prueba.bat"

Note: Regarding your previous intention to run this indefinitely, (which I do not recommend). When you start your .bat file, the Window Title, does not immediately register within tasklist.exe. That means, were you to run this in a loop or through a scheduled task, it is possible that the delay may cause your script to believe that the batch file isn't running, when in fact it is.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...