Created
February 19, 2017 08:22
-
-
Save aconn21/f64acccf589f8f4c088eca0af73b7cb7 to your computer and use it in GitHub Desktop.
PowerShell Stop Stuck Running Jobs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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