I have a PowerShell script that is run automatically when our monitoring service detects that a website is down. It is supposed to stop the AppPool (using Stop-WebAppPool -name $AppPool;
), wait until it is really stopped and then restart it.
Sometimes it the process does not actually stop, manifested by the error
Cannot Start Application Pool: The service cannot accept control messages at this time. (Exception from HRESULT: 0x80070425)"
when you try to start it again.
If it takes longer than a certain number of seconds to stop (I will chose that amount of time after I have timed several stops to see how long it usually takes), I want to just kill the process.
I know that I can get the list of processes used by workers in the AppPool by doing dir IIS:\AppPools\MyAppPool\WorkerProcesses\
,
Process ID State Handles Start Time ---------- ----- ------- ---------- 7124 Running
but I can't figure out how to actually capture the process id so I can kill it.
Open IIS and in the Connections panel select the server node. In Features View double click the Worker Processes and based on the Application Pool Name you will be able to determine the Process Id required. Go back to the Task Manager and match the Process Id you found in IIS with the PID in Task Manager.
Open IIS manager and on the left side click on the name of your computer. You will then see a similar list of icons on the right as shown in the screenshot below. Double click on "Worker Processes" and you can get a list of which processes are currently running, here you can find your second process.
Open the Windows Task Manager, ensure that the PID and Command Line columns are shown on the screen. For all w3wp.exe processes, check the value of the Command Line argument. Find the w3wp.exe process that has the target application pool name mentioned in the Command Line column.
Applications running in an IIS worker process (w3wp.exe) consume resources on the server and there are times when investigation to correlate the process ID (PID) to the application pool where the application is hosted is needed to understand the application consuming the resources.
In case that Process ID is really the id of process to kill, you can:
$id = dir IIS:\AppPools\MyAppPool\WorkerProcesses\ | Select-Object -expand processId Stop-Process -id $id
or
dir IIS:\AppPools\MyAppPool\WorkerProcesses\ | % { Stop-Process -id $_.processId }
In Command Prompt on the server, I just do the following for a list of running AppPool PIDs so I can kill them with taskkill or Task Mgr:
cd c:\windows\system32\inetsrv appcmd list wp
taskkill /f /pid *PIDhere*
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With