我想循環重複執行一個程序。Powershell啓動過程,等待超時,殺死並獲取退出代碼
有時,程序崩潰,所以我想殺了它,所以下一次迭代可以正確開始。我通過超時確定這一點。
我有超時工作,但無法獲得該程序的退出代碼,我也需要確定其結果。
之前,我並沒有等待超時,而是使用了 - 在Start-Process中等待,但是如果啓動的程序崩潰,這會使腳本掛起。有了這個設置,我可以正確地得到退出代碼。
我從ISE執行。
for ($i=0; $i -le $max_iterations; $i++)
{
$proc = Start-Process -filePath $programtorun -ArgumentList $argumentlist -workingdirectory $programtorunpath -PassThru
# wait up to x seconds for normal termination
Wait-Process -Timeout 300 -Name $programname
# if not exited, kill process
if(!$proc.hasExited) {
echo "kill the process"
#$proc.Kill() <- not working if proc is crashed
Start-Process -filePath "taskkill.exe" -Wait -ArgumentList '/F', '/IM', $fullprogramname
}
# this is where I want to use exit code but it comes in empty
if ($proc.ExitCode -ne 0) {
# update internal error counters based on result
}
}
我怎樣才能
- 啓動一個進程
- 等待它的有序進行和完成
- 殺死它,如果它的崩潰(如打超時)
- 獲取退出代碼的過程
【如何等待和殺死在PowerShell中的超時處理(HTTPS: //stackoverflow.com/q/19532998/995714) –