I’ve got a situation with a process spawned by MS SQL Server via XP_CMDShell .
XP_CMDShell is calling a PowerShell script, which then calls an executable to run an automated report.
I’m doing this so Powershell can kill the process if the executable hangs/takes too long for any reason.
This is working in general, but this morning I found a number of surviving processes of the executable that hadn’t died.
This process turned out to not be killable using either Powershell, or Taskkill from an Admin command prompt.
It was killable from WMIC or Task Manager.
Can anyone help me understand why? I’d rather have some chance of figuring out why it’s not killing cleanly.
Process followed:
Was expecting the Powershell scripting to kill the process successfully.
The relevant part of the PowerShell scripting that’s being used to kill this executable when it hangs:
$proc = Start-Process -FilePath $Executable -ArgumentList $Arguments -PassThru
Write-host $proc.id
Start-Sleep -seconds $MemcheckInterval #Give the process 2 seconds to start up and use memory
Write-host $proc.WorkingSet
$StaticCycles=0
$ProcessState="Working State" #Assume that the state is working ok
$Cycles=1 #Start the count of mem check cycles
$MemcheckValue=$proc.WorkingSet #Get the current working memory value
While((-not $proc.HasExited) -and ($Cycles -lt $MaxCycles) -and ($StaticCycles-lt $MaxStaticCycles) -and ((Get-Date)-lt $MaxTimeout))
{
Start-Sleep -seconds $MemcheckInterval
$Cycles=$Cycles+1
if (($MemcheckValue=$proc.WorkingSet) -and ($proc.WorkingSet-ne 0))
{$ProcessState="Static State"
$StaticCycles=$StaticCycles+1
}
Else {$ProcessState="Working State"}
}
Write-host $Cycles
Write-host $ProcessState
Write-host $proc.TotalProcessorTime
Write-host ((Get-Date)-$proc.StartTime)
write-host ($MaxTimeout)
if($proc.HasExited ) {
#Success
#Write-host "Detected Process already closed."
return 'Successful'
}
else {
#Failure
#Write-Host "Detected Process still running."
#Write-Host "Check for and kill process."
Write-host $proc.WorkingSet
$proc | Stop-Process -PassThru -Force -ErrorAction SilentlyContinue | Out-Null
return 'Timed Out'
}
Fine, so we got the ProcessID from Task Manager, and tried killing them:
First via Powershell:
Stop-Process 13636 -Force
This doesn’t work, and provides no useful information.
Then via an Administrator Command Prompt:
taskkill /F / PID 13636
This also didn’t work, but now we got an error:
ERROR: The process with PID 13636 could not be terminated.
Reason: There is no running instance of the task.
Except the task/process is visible from Task Manager.
Task Manager can kill the process via Right click End Task
WMIC can also kill the process from the same administrator command prompt we used previously using the command:
WMIC process where processid="13636" call terminate