태그 보관물: windows-service

windows-service

Windows 서비스가 충돌 할 경우 자동으로 다시 시작하려면 어떻게해야합니까? 있습니다. 충돌시 빠르게 다시 시작되도록 모니터링하는 간단한

며칠마다 예기치 않게 종료되는 Windows 서비스가 있습니다. 충돌시 빠르게 다시 시작되도록 모니터링하는 간단한 방법이 있습니까?



답변

서비스 애플리케이션에서 해당 서비스의 특성을 선택하십시오.

복구 탭을 봅니다. 모든 종류의 옵션이 있습니다. 첫 번째 및 두 번째 실패는 서비스를 다시 시작하지 못하도록 설정 하고, 세 번째는 BLAT 에서 세 번째 실패 알림이 포함 된 전자 메일을 발송 하는 배치 프로그램을 실행하도록 설정했습니다 .

또한 실패 횟수 재설정을 1로 설정하여 실패 횟수를 매일 재설정해야합니다.

편집하다:

명령 행을 통해이 작업을 수행 할 수있는 것 같습니다.

SC failure w3svc reset= 432000  actions= restart/30000/restart/60000/run/60000
SC failure w3svc command= "MyBatchFile.cmd"

MyBatchFile.CMD 파일은 다음과 같습니다.

blat - -body "Service W3svc Failed" -subject "SERVICE ERROR" -to Notify@Example.com -server SMTP.Example.com -f Administrator@Example.com

답변

Services.msc를 열고 서비스를 두 번 클릭하여 서비스 등록 정보를 엽니 다. 복구 탭이 있으며 이러한 설정을 사용하면 실패시 서비스를 다시 시작할 수 있습니다.


답변

복구 시간을 0으로 설정하십시오.

명령 행에 해당 :

SC failure YOUR_SERVICE_NAME reset= 0 actions= restart/0/restart/0/restart/0

어쨌든 자동 복구가 제대로 작동하지 않아 타사 소프트웨어를 사용하는 것이 좋습니다. 종료 코드가 0 인 서비스 종료가 정상적으로 종료되면 복구하려고하지 않는 것 같습니다.


답변

HostForLife.eu의 Windows 2008 서버에서 ServiceKeeper 를 사용 하고 있으며 매우 잘 작동합니다. 이전에는 ServiceHawk에 대한 검토가 있었지만보다 쉬운 관리 및 인터페이스를 위해 ServiceKeeper를 사용하는 것을 선호합니다.


답변

서비스를 중지해야 비슷한 요구 사항이있었습니다. 내가 생각한 가장 간단한 해결책은 5 분마다 Windows 작업 스케줄러에서 아래 명령을 실행하는 것입니다.

net start MyServiceName

이 명령은 기본적으로 서비스를 시작하고 (중지 된 경우) 서비스가 이미 실행중인 경우 적용되지 않습니다.


답변

나는 최근에 정해진 횟수만큼 서비스를 다시 시작하려고 시도하고 결론에 이메일 알림을 보내는 powershell 스크립트를 실행하는 복구 옵션을 구현했습니다.

여러 번의 시도 후 (그리고 내가 본 다른 모든 사항에도 불구하고) 서비스에서 복구 탭의 필드 구성은 다음과 같습니다.

프로그램 : Powershell.exe
** Not C : \ Windows \ System32 \ WindowsPowerShell \ v1.0 \ Powershell.exe

명령 줄 매개 변수 : -command “& {SomePath \ YourScript.ps1 ‘$ args [0]’ ‘$ args [1]’ ‘$ args [n]’}”

예 : -command “& {C : \ PowershellScripts \ ServicesRecovery.ps1 ‘서비스 이름’}”

** $ args는 스크립트로 전달 될 매개 변수입니다. 필요하지 않습니다.

다음은 powershell 스크립트입니다.

cd $PSScriptRoot

$n = $args[0]

function CreateLogFile {
$events = Get-EventLog -LogName Application -Source SomeSource -Newest 40
if (!(Test-Path "c:\temp")) {
    New-Item -Path "c:\temp" -Type directory}
if (!(Test-Path "c:\temp\ServicesLogs.txt")) {
    New-Item -Path "c:\temp" -Type File -Name "ServicesLogs.txt"}
    $events | Out-File -width 600 c:\temp\ServicesLogs.txt
}

function SendEmail  {
$EmailServer = "SMTP Server"
$ToAddress = "Name@domain.com"
$FromAddress = "Name@domain.com"

CreateLogFile

$Retrycount = $Retrycount + 1
send-mailmessage -SmtpServer $EmailServer -Priority High -To $ToAddress -From $FromAddress -Subject "$n Service failure" `
-Body "The $n service on server $env:COMPUTERNAME has stopped and was unable to be restarted after $Retrycount attempts." -Attachments c:\temp\ServicesLogs.txt

Remove-Item "c:\temp\ServicesLogs.txt"
}

function SendEmailFail  {
$EmailServer = "SMTP Server"
$ToAddress = "Name@domain.com"
$FromAddress = "Name@domain.com"

CreateLogFile

$Retrycount = $Retrycount + 1
send-mailmessage -SmtpServer $EmailServer -Priority High -To $ToAddress -From $FromAddress -Subject "$n Service Restarted" `
-Body "The $n service on server $env:COMPUTERNAME stopped and was successfully restarted after $Retrycount attempts. The relevant system logs are attached." -Attachments c:\temp\ServicesLogs.txt

Remove-Item "c:\temp\ServicesLogs.txt"
}

function StartService {

$Stoploop = $false

do {
   if ($Retrycount -gt 3){
     $Stoploop = $true
     SendEmail
     Break
    }

   $i =  Get-WmiObject win32_service | ?{$_.Name -imatch $n} | select Name, State, StartMode
    if ($i.State -ne "Running" -and $i.StartMode -ne "Disabled") {

        sc.exe start $n
        Start-Sleep -Seconds 35

        $i =  Get-WmiObject win32_service | ?{$_.Name -imatch $n} | select State
          if ($i.state -eq "Running"){
              $Stoploop = $true
              SendEmailFail}
          else {$Retrycount = $Retrycount + 1}
    }
}
While ($Stoploop -eq $false)
}

[int]$Retrycount = "0"
StartService

답변

이것은 비슷한 스레드 에 대한 나의 대답이었습니다 .

이와 같은 간단한 vbs 스크립트를 예약하여 필요한 경우 컴퓨터에서 주기적으로 서비스를 다시 시작할 수 있습니다.

strComputer = "."
strSvcName = "YOUR_SERVICE_NAME"
objWMI = GetObject ( "winmgmts : \\"& strComputer & "\ root \ cimv2") 설정
objService = objWMI.Get ( "Win32_Service.Name = '"& strSvcName & "'") 설정
objService.State = "중지 된"경우
    objService.StartService ()
끝 경우