[英]How to RESTART a specific command if it fails in Windows Powershell?
In a ps1 file I start 2 separate processes like so: As you can see the only difference is the CPU affinity.在 ps1 文件中,我启动了 2 个独立的进程,如下所示: 如您所见,唯一的区别是 CPU 亲和性。
$Process1 = Start-Process -FilePath 'C:\Windows\system32\notepad.exe' -PassThru
$Process1.ProcessorAffinity = 65535
$Process1.PriorityClass = 'RealTime'
$Process2 = Start-Process -FilePath 'C:\Windows\system32\notepad.exe' -PassThru
$Process2.ProcessorAffinity = 4294901760
$Process2.PriorityClass = 'RealTime'
Sometimes, randomly, one of the 2 processes fails with: Problem Event Name: BEX64有时,随机地,两个进程之一失败:问题事件名称:BEX64
It is essential that the process is restarted immediately when a failure like this happens.发生此类故障时,必须立即重新启动进程。 So I am faced with this problem.
所以我面临这个问题。
Can I monitor within the PS1 file the 2 processes and if one fails to restart it?
我可以在 PS1 文件中监视 2 个进程,如果一个进程无法重新启动它吗? The monitoring process must be able to distinguish which of the 2 processes has failed, so that the correct affinity is used to restart it.
监控进程必须能够区分这 2 个进程中的哪个进程失败了,以便使用正确的亲和性来重新启动它。
If you have a solution involving windows batch script please visit How to RESTART a specific command if it fails in Windows Batch file?如果您有涉及 Windows 批处理脚本的解决方案,请访问如何在 Windows 批处理文件中失败时重新启动特定命令?
Thanks谢谢
$Process1 = Start-Process -FilePath 'C:\Windows\system32\notepad.exe' -PassThru
$Process1.ProcessorAffinity = 1
$Process1.PriorityClass = 'RealTime'
Write-Host Process ID $process1.Id started with CPU affinity $Process1.ProcessorAffinity
$Process2 = Start-Process -FilePath 'C:\Windows\system32\notepad.exe' -PassThru
$Process2.ProcessorAffinity = 2
$Process2.PriorityClass = 'RealTime'
Write-Host Process ID $process2.Id started with CPU affinity $Process2.ProcessorAffinity
while($true){
foreach($process in $Process1,$Process2){
if($process.hasexited){
Write-Host "Process ID $($process.id) has exited, restarting."
$priority = $process.PriorityClass
$affinity = $process.ProcessorAffinity
if($newprocess = $process.Start()){
Write-Host Successfully restarted process ID $process.Id with CPU affinity $process.ProcessorAffinity
$process.PriorityClass = $priority
$process.ProcessorAffinity = $affinity
Write-Host Priority Class set back to $process.PriorityClass
Write-Host Process affinity set back to $process.ProcessorAffinity
}
}
}
Start-Sleep -Milliseconds 500
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.