次の方法で共有


Terminate w3wp.exe process without stopping website

Here is a quick way to terminate w3wp.exe process in Azure App Service Website without stopping the whole website.

Note : Azure AppService website can run on multiple instances and there is another w3wp.exe process called SCM running on each instance (we shouldn’t kill this process). So we just have to get list of all instances running the website using Get-AzureRMResource cmdlet. On each instance, we need to get list of processes running using same Get-AzureRMResource cmdlet. Next, we need to check if this process is w3wp.exe process and see if the SCM property is true. Last step is using Remove-AzureRMResource  to terminate the process.

Azure Resource Explorer is the best tool that can help us to pick the right PowerShell cmdlet to use and what parameters to pass. Just click on PowerShell tab in the resource explorer as shown below.

image

 

Login-AzureRmAccount

$resoureGroupName = "wabac"
$websiteName = "wabacblue"


$instances = Get-AzureRmResource -ResourceGroupName $resoureGroupName `
                                 -ResourceType Microsoft.Web/sites/instances `
                                 -ResourceName $websiteName `
                                 -ApiVersion 2018-02-01


foreach($instance in $instances)
{
    $instanceName = $instance.Name

    Write-Host "Site Instance Name : " $instanceName

    $processes = Get-AzureRmResource -ResourceGroupName $resoureGroupName `
                                    -ResourceType Microsoft.Web/sites/instances/processes `
                                    -ResourceName $websiteName/$instanceName `
                                    -ApiVersion 2018-02-01

    foreach($process in $processes)
    {
        $exeName = $process.Properties.Name
        Write-Host "`tEXE Name : " $exeName

        if($exeName -eq "w3wp")
        {
            $processId = $process.Properties.id
            Write-Host "`t`tProcess ID : " $processId

            $processDetails = Get-AzureRmResource -ResourceGroupName $resoureGroupName `
                                                    -ResourceType Microsoft.Web/sites/instances/processes `
                                                    -ResourceName $websiteName/$instanceName/$processId `
                                                    -ApiVersion 2018-02-01

            Write-Host "`t`tSCM Value : " $processDetails.Properties.is_scm_site
            
            if($processDetails.Properties.is_scm_site -ne $true)
            {
                Write-Host "`t`t`tNot a SCM Process : " $process.Properties.Name

                $deleted = Remove-AzureRmResource -ResourceGroupName $resoureGroupName `
                                                    -ResourceType Microsoft.Web/sites/instances/processes `
                                                    -ResourceName $websiteName/$instanceName/$processId `
                                                    -ApiVersion 2018-02-01 -Force

                if($deleted -eq $true)
                {
                    Write-Host "`t`t`t`tW3wp process killed" -ForegroundColor Green
                }
                else 
                {
                    Write-Host  "`t`t`t`tFailed to kill W3wp process" -ForegroundColor Red
                }
            }
        }
    }
}