Taskkill.exe: don't throw an error if the process is not running

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?

8 Answers

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

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

This works well too:

taskkill /IM "w3wp.exe" /F /FI "STATUS eq RUNNING"
3

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

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

Info from: here

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.

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

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

see:

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

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

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like