Created
February 13, 2025 10:42
-
-
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)
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
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