48

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?

Luk
  • 1,107

9 Answers9

33

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
Mokubai
  • 95,412
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

26

This works well too:

taskkill /IM "w3wp.exe" /F /FI "STATUS eq RUNNING"
techraf
  • 4,952
12

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

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

Info from: here

acarlon
  • 221
7

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.

Derek
  • 211
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.
nicovota
  • 161
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

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

taskkill /im test.exe 2>nul
Basj
  • 2,143
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