使用powershell监控命令行console程序并在停止时启动

有一种C#命令行console程序,为了能看到console台的输出所以不能做成服务。为了防止这些程序自己死掉,使用powershell监控程序并重启

 

#利用程序名来进行重启
if (!(get-process | where-object {$_.mainwindowtitle -eq '管理员:  "tsiphone"'}))
{ cd C:\lengshan
  C:\lengshan\tsiphone.bat }
  
  

#如果是在命令行参数里面执行的话,那就应该适应 gwmic 来获得comandline的内容来进行区分了
#因为正常情况下,get-process的操作是没有comandline这个属性的


if (!(Get-WmiObject win32_process | where {$_.CommandLine -eq "`"C:\php\php.exe`"  `"C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\deamon\igetui_deamon.php`""}))


{ cmd /c start "" "C:\lengshan\tsgetui.bat"  }
  
#将命令行做成bat,之后利用powershell判断进程是否存在,不存在的话,先结束对应的cmd进程,之后再利用start开启新的cmd窗口来启动php进程
if (!(Get-WmiObject win32_process | where {$_.CommandLine -eq "`"c:\php\php.exe`"  `"C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\deamon\apns_deamon.php`""}))
{ get-process cmd |where {$_.MainWindowTitle -eq "管理员:  `"tsiphone`""} | stop-process
  cmd /c start "" "C:\lengshan\tsiphone.bat"}

posted @ 2014-08-06 14:47  firtree  阅读(2039)  评论(0编辑  收藏  举报