I'm trying to watch a process in a background job, but i think i don't understand background processing in powershell correctly. I'm assuming that because my script doesn't want to work.
$target = "firefox"
$Watch = {
while (1) {
sleep -Milliseconds 100
if(!((get-process $target -ErrorAction SilentlyContinue).Responding -eq $true))
{
Get-Job -name $target | Stop-Job
break
}
}
}
Start-Job -Name $target -ScriptBlock $Watch
In my opinion, this script should control the "responding" property of my "firefox" process each 100 milliseconds and if firefox hangs or gets closed it should stop the background job.
if I execute this script and then close my firefox process, the Job is still running. It seems like it doesn't care about my Stop-Job command.
if i execute this scipt without any jobs like this, it works like a charm.
$target = "firefox"
while (1) {
sleep -Milliseconds 100
if(!((get-process $target -ErrorAction SilentlyContinue).Responding -eq $true))
{
Write-Host "Hello"
break
}
}
if i run this script with jobs, and do the get-job -name $target | stop-job part in the console, it also works. so it just doesn't want to execute my if () {scriptblock} while running as a background job.
is it impossible to run a loop in a Background-Job?