Computer >> Hướng Dẫn Máy Tính >  >> Hệ Thống >> Máy Chủ Windows

Cách tự động khởi động lại App/Process bị crash hoặc đóng bằng PowerShell

Hãy học cách dùng PowerShell để kiểm tra xem một ứng dụng hoặc tiến trình cụ thể có đang chạy không; cách tự động khởi động lại nó trong trường hợp crash, nếu user đóng nó vô tình, hoặc nó bắt đầu tiêu tốn lượng lớn bộ nhớ (memory leak).

Trước đây chúng tôi đã chỉ cách quản lý tiến trình Windows bằng PowerShell. Để đảm bảo tiến trình notepad.exe đang chạy và khởi động lại nó, bạn có thể dùng script dưới đây:
If (!(Get-Process -Name notepad -ErrorAction SilentlyContinue)) {Invoke-Item C:\Windows\notepad.exe }

Bạn có thể tự động khởi động lại tiến trình nếu nó không phản hồi (bị treo) hoặc nếu nó bắt đầu dùng quá nhiều bộ nhớ (hơn 1000 MB trong ví dụ này):

$proc = Get-Process -Name notepad| Sort-Object -Property ProcessName -Unique
If (($proc.Responding -eq $false) –or ($proc.WorkingSet -GT 1000000*1024)) {
$proc.Kill()
Start-Sleep -s 10
Invoke-Item C:\Windows\notepad.exe
}

Sử dụng vòng lặp for PowerShell, bạn có thể tạo vòng lặp vô tận khởi động tiến trình, kiểm tra mỗi 60 giây xem nó có đang chạy không, và khởi động lại nếu cần:

for(;;){
try{
If (!(Get-Process -Name notepad -ErrorAction SilentlyContinue))
{Invoke-Item C:\Windows\notepad.exe}
$proc = Get-Process -Name notepad | Sort-Object -Property ProcessName -Unique -ErrorAction SilentlyContinue
If (!$proc -or ($proc.Responding -eq $false) –or ($proc.WorkingSet -GT 200000*1024)) {
$proc.Kill()
Start-Sleep -s 10
Invoke-Item C:\Windows\notepad.exe}
}
catch { }
Start-sleep -s 60
}

Nếu bạn muốn kiểm tra trạng thái tiến trình trên máy tính từ xa, bạn có thể dùng lệnh này:

$proc = Get-Process -ComputerName WKS-NYC211 -Name notepad | Sort-Object -Property ProcessName -Unique -ErrorAction SilentlyContinue

Để khởi động tiến trình từ xa, bạn có thể dùng cmdlet Invoke-Command:

Invoke-Command -ComputerName WKS-NYC211 -Credential $Cred -ScriptBlock {Start-Process C:\Windows\notepad.exe -wait -verb runas;}

Bạn có thể chạy script PowerShell này như GPO logon script khi user đăng nhập.

Sau đó lưu code PowerShell vào file với đuôi *.PS1. Bạn có thể ký script bằng chữ ký số, thay đổi cài đặt PowerShell Execution policy, hoặc chạy script với tùy chọn –ExecutionPolicy Bypass.

  • Tên file: %windir%\System32\WindowsPowerShell\v1.0\powershell.exe
  • Tùy chọn chạy: -windowstyle hidden -ExecutionPolicy Bypass –Noprofile -file %~dp0CheckProcess.ps1

Bạn cũng có thể chạy script PS1 theo lịch bằng Task Scheduler. Dùng cùng tùy chọn chạy. Bạn cũng có thể chỉ định tài khoản user bạn muốn chạy tiến trình.

$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-windowstyle hidden -ExecutionPolicy Bypass -file %windir%\CheckProcess.ps1"
$Trigger= New-ScheduledTaskTrigger -AtLogon
$Principal=New-ScheduledTaskPrincipal -UserId "jsmith" -LogonType Interactive
$Task=New-ScheduledTask -Action $Action -Trigger $Trigger -Principal $Principal
Register-ScheduledTask -TaskName "Check Notepad Process" -InputObject $Task

Hoặc bạn có thể chạy script PowerShell này như một Windows service.

Nếu ứng dụng chạy không cần tương tác với user, tốt hơn là chạy nó như một service. Sau đó, bạn sẽ có thể quản lý nó qua console services.msc tiêu chuẩn hoặc PowerShell. Windows có tính năng tích hợp để restart service, hoặc bạn có thể restart service bị treo như sau.