Skip to content

Instantly share code, notes, and snippets.

@navchandar
Created July 26, 2021 06:41
Show Gist options
  • Select an option

  • Save navchandar/50f257b10c4b6a585d0fe5052e362f84 to your computer and use it in GitHub Desktop.

Select an option

Save navchandar/50f257b10c4b6a585d0fe5052e362f84 to your computer and use it in GitHub Desktop.
# 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