Tip/Trick: List Running ASP.NET Worker Processes and Kill/Restart them from the command-line

Problem

You want a quick way to kill a process on your system, or kill and restart an ASP.NET or IIS worker process. 

Solution

Windows has two built-in command-line utilities that you can use to help with this: Tasklist and Taskkill

Within a command-line window you can type "Tasklist" to obtain a listing of all of running Windows processes on your system:

C:\Documents and Settings\Scott>tasklist

Image Name                   PID Session Name     Session#    Mem Usage
========================= ====== ================ ======== ============
System Idle Process            0 Console                 0         16 K
System                         4 Console                 0        212 K
smss.exe                     824 Console                 0        372 K
csrss.exe                    876 Console                 0      5,116 K
winlogon.exe                 900 Console                 0      3,848 K
services.exe                 944 Console                 0      4,112 K
lsass.exe                    956 Console                 0      1,772 K
svchost.exe                 1372 Console                 0     22,240 K
svchost.exe                 1524 Console                 0      3,428 K
svchost.exe                 1572 Console                 0      4,916 K
spoolsv.exe                 1788 Console                 0      5,660 K
inetinfo.exe                 352 Console                 0      9,860 K
sqlservr.exe                 612 Console                 0      7,348 K
sqlservr.exe                 752 Console                 0     15,552 K
explorer.exe                2960 Console                 0     25,224 K
CTHELPER.EXE                3660 Console                 0      4,964 K
LVComS.exe                   872 Console                 0      3,092 K
msmsgs.exe                  3596 Console                 0      6,532 K
sqlmangr.exe                3096 Console                 0      4,264 K
OUTLOOK.EXE                 1740 Console                 0     75,992 K
iexplore.exe                 472 Console                 0     37,372 K
cmd.exe                      732 Console                 0      2,436 K
tasklist.exe                3104 Console                 0      4,156 K
wmiprvse.exe                3776 Console                 0      5,416 K

TaskKill can then be used to terminate any process instance in the above list.  Simply provide it with the PID (Process ID) value of the process instance to kill and it will terminate it:

C:\Documents and Settings\Scott>taskkill /pid 1980
SUCCESS: The process with PID 1980 has been terminated.

ASP.NET on Windows 2000 and XP runs code within the "aspnet_wp.exe" worker process (when using IIS).  On Windows 2003 it runs within the IIS6 "w3wp.exe" worker process.  Both of these processes are launched from Windows system services, which means you must provide the "/F" switch to taskkill to force-terminate them:

C:\Documents and Settings\Scott>tasklist

Image Name                   PID Session Name     Session#    Mem Usage
========================= ====== ================ ======== ============
aspnet_wp.exe               3820 Console                 0     13,512 K

C:\Documents and Settings\Scott>taskkill /pid 3820 /F
SUCCESS: The process with PID 3820 has been terminated.

As a short-cut, you can also just provide the process image name to "Taskkill" if you want to avoid having to lookup the PID value for a specific process instance.  For example, the below command will kill all ASP.NET worker processes on the system:

C:\Documents and Settings\Scott>taskkill /IM aspnet_wp.exe /F
SUCCESS: The process "aspnet_wp.exe" with PID 2152 has been terminated.

ASP.NET and IIS will automatically launch a new worker process the next time a request is received by the system.  So when you run the above command it will shutdown all active ASP.NET Worker processes.  When you then hit the site again a new one will be automaticlaly launched for it (and it will have a new PID as a result).

Note that both TaskList and TaskKill support a "/S" switch that allows you to specify a remote system to run the commands against.  If you have remote admin rights on one of your co-workers machines this can be a lot of fun.

Credits

The above tutorial was inspired by Steve Lamb's nice post here.  I was surprised to discover that these commands work with Windows XP and Windows 2003 too.  Since the mid-90s I've been using a private set of utilities I always install on my dev box to accomplish the same task, without realizing that somewhere along the way they've been built-into Windows.  Many thanks to Steve for pointing them out.

Hope this helps,

Scott

 

9 Comments

  • The tip is certainly easiest with Process Explorer (sysinternal) and with the "Command Line" column shown (but it not works on remote system :P )

  • In the same vein, iisapp is a very useful script that will tell you which worker process is serving which IIS application. Useful when you don't want to kill the wrong application :-)

    Of course this is only useful when using a version of IIS that supports application pools (so XP users are out of luck).

  • Hi Christoph,

    In the samples above I'm only killing the IIS worker processes -- and not the InetInfo process itself (which is where configuration and things like SSL certs and routed through).

    One benefit of this is that you can't loose any state when killing the process (because they are stateless).

    The other benefit (which is an advantage over iisreset) is that the web-server never goes down -- so incoming requests won't be lost.

    Hope this helps,

    Scott

  • nah. in a perfect world, you dont loose an request. but in the real, when you dont have a that simple app, there will be some requests broken. and doesn't it recompile the aspx files after that kill? and the sessionstate (if inproc) is stored in the inetinfo process, or?

  • What if you have huge amount of people connected to IIS who uses (not direct ofcourse but throught web applications) aspnet_wp. You can't simply kill process. If you do all of them gonna lose their session. Isn't a better way to solve this problem ?

  • I usually just use "iisreset" and kill both IIS and aspnet.

  • Hi Christoph,

    Compilations are cached on disk -- so no recompiles happen when a process is restarted. If session state is stored inproc then it will be lost. If you use the external state store option it will be preserved.

    Hope this helps,

    Scott

  • Hi scott,

    try this, It will kill even un-killable process too.

    ntsd -p [pid] -c "q"

    Happy coading

    Dhananjay

  • Sander also posted a useful link to checkout here: http://todotnet.com/archive/2006/07/02/7491.aspx

    Thanks,

    Scott

Comments have been disabled for this content.