Created
July 26, 2021 06:41
-
-
Save navchandar/50f257b10c4b6a585d0fe5052e362f84 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # Script to retrigger scripts if crashed | |
| function trigger-script{ | |
| #$proc = Start-Process notepad.exe -Passthru | |
| $proc = Start-Process Powershell.exe -argument "C:\Scripts\Script_Name.ps1 ScriptArugments" -Passthru | |
| return $proc | |
| } | |
| function check-process{ | |
| [CmdletBinding()] | |
| param([Parameter(Mandatory)] [Int32]$ID) | |
| $Running = Get-Process -ID $ID -ErrorAction SilentlyContinue | |
| if ($Running){ | |
| return $true | |
| } | |
| return $false | |
| } | |
| $proc = trigger-script | |
| $status = check-process $proc.ID | |
| while ($status) { | |
| Start-Sleep -Seconds 2 | |
| $status = check-process $proc.ID | |
| if (-not $status){ | |
| Write-Host "$proc.Name Process $proc.ID stopped. Restarting now" -ForegroundColor Yellow | |
| # Avoid multiple same processes - Kill safely if running | |
| Stop-Process -id $proc.ID -Force -ErrorAction SilentlyContinue | |
| Start-Sleep -Seconds 2 | |
| $proc = trigger-script | |
| $status = check-process $proc.ID | |
| } else { | |
| Write-Host "$proc.Name Process $proc.ID running." -ForegroundColor Green | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment