使用powershell監控命令列console程式並在停止時啟動

來源:互聯網
上載者:User

標籤:使用   io   art   ar   div   line   管理   window   

有一種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"}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.