--
windows 2012 監測某個程式沒有回應
ChatGPT 給出了可以使用 PowerShell 的建議,不用使用其他程式、執行指令也不會錯誤,有可能可以滿足需求。
--
使用 PowerShell 寫出一個偵測 WebPaperMake01 是否沒有回應 並重新執行的批次檔
於是提出實際需求,偵測某個程式並且沒有回應時重新執行。
ChatGPT 分成兩個部份 PowerShell .ps1 和批次檔 .bat
--
monitor_WebPaperMake01.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# 設定程式名稱(不含 .exe) $processName = "WebPaperMake01" # 程式完整路徑(根據你的安裝位置調整) $exePath = "C:\Path\To\WebPaperMake01.exe" # 嘗試取得該程序 $proc = Get-Process -Name $processName -ErrorAction SilentlyContinue # 如果程序存在 if ($proc) { if (-not $proc.Responding) { Write-Output "$(Get-Date): $processName 無回應,準備重新啟動..." # 結束程序 $proc.Kill() Start-Sleep -Seconds 3 # 重新啟動 Start-Process -FilePath $exePath Write-Output "$(Get-Date): $processName 已重新啟動" } else { Write-Output "$(Get-Date): $processName 正常運作中" } } else { Write-Output "$(Get-Date): $processName 未執行,準備啟動..." Start-Process -FilePath $exePath Write-Output "$(Get-Date): $processName 已啟動" } |
--
run_monitor.bat
1 2 3 4 5 |
@echo off :LOOP powershell -ExecutionPolicy Bypass -File "C:\Path\To\monitor_WebPaperMake01.ps1" timeout /t 30 >nul goto LOOP |
--
122 total views, 4 views today