45

I'm running a Visual Studio post build event that shuts down IIS if it's still running after compilation:

taskkill /f /im w3wp.exe

The following works perfectly if IIS is still running, but throws an error if it has already stopped:

Error 1 The process "w3wp.exe" not found. xxx\EXEC

Is there a way to tell taskkill to ignore the problem if it can't find a matching running process?

9 Answers 9

31

Instead of running one command, would running a small batch file work instead?

tasklist /FI "IMAGENAME eq w3wp.exe" 2>NUL | find /I /N "w3wp.exe">NUL
if "%ERRORLEVEL%"=="0" taskkill /f /im w3wp.exe
0
30

The solution I found to this was to run

START /wait taskkill /f /im w3wp.exe

It returns a success from the START command, and any error thrown by TASKKILL is thrown in the new console window

2
  • 6
    and if you want to wait for tasks to be killed before continuing with the batch file. simply add: START /wait taskkill /f /im w3wp.exe
    – jezternz
    Jan 20, 2016 at 23:04
  • 1
    The wait is a very important detail. I will edit that into the original answer to avoid confusion. Thanks @jezternz ! Jan 7, 2019 at 18:06
22

This works well too:

taskkill /IM "w3wp.exe" /F /FI "STATUS eq RUNNING"
3
  • I could not get this to work - my version of taskkill does not seem to recognise "STATUS eq RUNNING". However, "MEMUSAGE gt 2" does work.
    – Polyfun
    Aug 18, 2017 at 9:15
  • I found that "STATUS eq UNKNOWN" was a better option for catching processes that may or may not be stuck (but either way, you don't want them) Sep 7, 2018 at 13:39
  • "STATUS ne UNKNOWN" -> not equal unknown -> kill if it exists was working for me. Jul 7, 2021 at 14:16
11

Or, this will just return an info message if not found:

taskkill /f /im w3wp.exe /fi "memusage gt 2"

Info from: here

5

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.

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

This is the output in case the process was running:

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

This is the output in case not running:

process "w3wp.exe" not running.
5

For a prebuild event in visual studio this worked for me:

taskkill /f /im scriptcode.exe 2>nul 1>nul
exit 0

see: https://stackoverflow.com/questions/5806444/swallowing-errors-in-pre-build-steps-in-visual-studio-2010

I had some problems with status = running checks, process existed but did not have status = running. I was using that before.

1

Forces the process to kill (if the process is running), and don't output any information.

The following command work fine.

taskkill /f /im w3wp.exe /t /fi "status eq running">nul

1
  • How will this kill w3wp.exe? May 10, 2018 at 14:38
1

If all you need is to not display the error, this works:

taskkill /im test.exe 2>nul
0

Short Answer:

Append || exit 0

Details

I cannot test the exact case given, but I found this works:

pskill w3wp.exe || exit 0

The '||' says run the second command if the first fails. A ';' should work as well.

Additionally to totally silence a command use something like:

rem totally silent
rem order matters. Redirect then join
some_cmd 1>nul: 2>>&1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .