Wednesday, November 25, 2009

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

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:

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:

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:

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:

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.

No comments: