简体   繁体   English

如果在 Windows Powershell 中失败,如何重新启动特定命令?

[英]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.

相关问题 如何在powershell中为windows进程重新启动docker? - How to restart docker for windows process in powershell? 从 Windows PowerShell 中的命令中提取特定值 - Extract specific value from command in Windows PowerShell 任何重新启动WebRole的PowerShell命令 - Any PowerShell Command to Restart WebRole 重新启动后如何使用(Windows Powershell)GitShell命令历史记录? - How to use (Windows Powershell) GitShell commands history after restart? 如何使用Windows PowerShell中的Compress-Archive命令循环访问特定文件名? - How can I loop through specific file names with the Compress-Archive command in Windows PowerShell? windows powershell命令卸载除特定版本之外的所有版本的java - windows powershell command to uninstall all versions of java except a specific version 如何从Powershell命令输出特定信息? - How to output specific information from a powershell command? 如何在Windows powershell命令中使用“ OR”和管道进行findstr - How to use “OR” and pipe in windows powershell command for findstr 在Powershell中使用变量关闭/重新启动命令 - Shutdown/restart command using a variable in powershell Windows Task Scheduler Powershell脚本完成并重新启动 - Windows Task Scheduler Powershell Script complete and restart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM