Skip to content

Instantly share code, notes, and snippets.

@aconn21
Created February 19, 2017 08:22
Show Gist options
  • Save aconn21/f64acccf589f8f4c088eca0af73b7cb7 to your computer and use it in GitHub Desktop.
Save aconn21/f64acccf589f8f4c088eca0af73b7cb7 to your computer and use it in GitHub Desktop.
PowerShell Stop Stuck Running Jobs
$ThreadTimeout = 300
#Check for jobs we can timeout
$RunningJobs = get-job -State Running
foreach($RunningJob in $RunningJobs)
{
$CurrentTime = get-date
$TimeoutTime = $RunningJob.PSBeginTime
$TimeoutTime = $TimeoutTime.AddSeconds($ThreadTimeout)
#The equation here is:
#if the current time is more than the time the job started + 5 minutes (300 seconds)
#then its time we get the info about the job and then stop it
if($CurrentTime -gt $TimeoutTime)
{
$Log = @()
$Log += ("Killing stuck thread " + $RunningJob.Name)
$Log += $RunningJob.Output
$Log += $RunningJob.Error
echo $Log
$Log >> $LogFileName
#Stop the job
$RunningJob | Stop-Job
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment