简体   繁体   中英

Kill IF process exist in Windows

I want to kill a process ONLY IF the process exists, I want to do this because I have a tool that runs only if there is no error and I made some tests and indeed there is an error when I try to kill a process that doesn't exists. This is the line I use to kill a process:

taskkill /f /IM notepad.exe

But I want to validate it only executes when the process EXISTS.

Thank you!!!

Taken from this question, you should try this code:

taskkill /f /im notepad.exe /fi "memusage gt 40" 2>NUL | findstr SUCCESS >NUL && if errorlevel 1 ( echo Notepad was not killed ) else ( echo Notepad was killed )

If that does not help, you can look at this and this question.

I hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM