Skip to content

Instantly share code, notes, and snippets.

@hazg
Created February 13, 2025 10:42
Show Gist options
  • Save hazg/431ae29e9f8441ca9217b130273b6145 to your computer and use it in GitHub Desktop.
Save hazg/431ae29e9f8441ca9217b130273b6145 to your computer and use it in GitHub Desktop.
restarts a Windows program if its output contains a certain text (e.g. error)
Set-Location "C:\\path\\to\\program" # Path to exe
$processName = "lan-mouse" # Name (do not include .exe)
$argumentList = "-d" # Arguments
$timeout = 5 # Watch timeout in seconds
$logFile = "$processName-log.txt"
function Start-ProcessAndMonitor {
Start-Process -FilePath $processName -ArgumentList $argumentList -NoNewWindow -RedirectStandardError $logFile
Start-Sleep -Seconds 5
}
function MonitorOutput {
$output = Get-Content -Path $logFile -Tail 10
if ($output -match "webrtc") {
Write-Host "Error detected, restarting $processName"
Stop-Process -Name $processName -Force
Start-ProcessAndMonitor
}
}
Start-ProcessAndMonitor
while ($true) {
Start-Sleep -Seconds $timeout
MonitorOutput
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment