【发布时间】:2016-02-22 09:31:32
【问题描述】:
我正在尝试查看后台作业中的进程,但我认为我无法正确理解 powershell 中的后台处理。我假设是因为我的脚本不想工作。
$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
在我看来,这个脚本应该每 100 毫秒控制我的“firefox”进程的“响应”属性,如果 firefox 挂起或关闭,它应该停止后台作业。
如果我执行此脚本然后关闭我的 firefox 进程,则作业仍在运行。它似乎并不关心我的Stop-Job 命令。
如果我在没有任何类似工作的情况下执行这个 scipt,它就像一个魅力。
$target = "firefox"
while (1) {
sleep -Milliseconds 100
if(!((get-process $target -ErrorAction SilentlyContinue).Responding -eq $true))
{
Write-Host "Hello"
break
}
}
如果我使用作业运行此脚本,并在控制台中执行get-job -name $target | stop-job 部分,它也可以工作。所以它只是不想在作为后台作业运行时执行我的if () {scriptblock}。
是否不可能在后台作业中运行循环?
【问题讨论】:
标签: powershell jobs