プログラムをループで繰り返し実行したい。
ときどきプログラムがクラッシュするので、次の反復を正しく開始できるようにプログラムを強制終了します。これはタイムアウトで判断します。
タイムアウトは機能していますが、プログラムの終了コードを取得できません。これも結果を判断する必要があります。
以前は、タイムアウトで待機せず、Start-Process で -wait を使用しただけでしたが、これにより、起動したプログラムがクラッシュした場合にスクリプトがハングしていました。このセットアップでは、終了コードを正しく取得できました。
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
}
}
どうやって
- プロセスを開始する
- 正常に実行されて終了するのを待ちます
- クラッシュした場合は強制終了します (例: タイムアウトに達した場合)。
- プロセスの終了コードを取得する