如何在while循环中监视两个进程时知道哪个进程死了

Sim*_*onS 1 powershell

我正在监视服务器上的两个进程.当其中一个人去世时,我需要知道它是哪一个.

有没有比我到目前为止检查这个更好的方法?有没有办法做到这一点没有if/elseif我到目前为止?

while ((ps proc1 -ov websrv).Responding -and (ps proc2 -ov connec).Responding) 
{ sleep -m 100 }

$pmsname = if (!$websrv.Responding -and !$connec.Responding) { "beide" }
            elseif (!$websrv.Responding -and $connec.Responding) { "websrv" }
            elseif ($websrv.Responding -and !$connec.Responding) { "connec" }
Run Code Online (Sandbox Code Playgroud)

Ans*_*ers 6

你可以这样做:

$status = [int]$websrv.Responding + [int]$connec.Responding * 2

$pmsname = switch ($status) {
  0 { 'keiner' }
  1 { 'websrv' }
  2 { 'connec' }
  3 { 'beide' }
  default { throw "unrecognized status: $status" }
}
Run Code Online (Sandbox Code Playgroud)

Responding如果进程正在响应,则将属性的值转换为整数会使值为1,如果不响应则为0.通过将该数字乘以第二个进程的2,可以使两个进程的"响应"状态相互区分,以便您可以添加值并使用switch语句来确定总体状态.