如何在崩溃的情况下自动重启Windows服务?

我有一个Windows服务,每隔几天就意外退出。 有没有一种简单的方法来监视它,以确保它在崩溃时迅速重新启动?

在“服务”应用程序下,select相关服务的属性。

查看恢复选项卡 – 有各种选项 – 我会设置第一个和第二个失败重新启动服务,第三个运行一个批处理程序, 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 [email protected] -server SMTP.Example.com -f [email protected] 

打开Services.msc,双击服务打开服务的属性,有一个恢复选项卡,这些设置应允许您在失败时重新启动服务。

这是我在类似的线程上的答案希望这可以帮助…

您可以安排这样一个简单的vbs脚本,以便在需要时定期重新启动计算机上的服务。

 strComputer =“。” 
 strSvcName =“YOUR_SERVICE_NAME” 
 set objWMI = GetObject(“winmgmts:\\”&strComputer&“\ root \ cimv2”)
 set objService = objWMI.Get(“Win32_Service.Name ='”&strSvcName&“'”)
如果objService.State =“停止”那么
     objService.StartService()
万一

有人在超级用户问过类似的问题:你可以安装一个监视Windows服务的工具。 像“ 服务之鹰”(Service Hawk)这样的服务可以帮助您保持服务的开始,或者让您安排自动重启(可能在夜间)以保持服务顺利运行。

我在HostForLife.eu的Windows 2008服务器上使用ServiceKeeper ,它工作的非常好。 以前,我对ServiceHawk进行了一次审查,但是我更喜欢使用ServiceKeeper来实现更简单的pipe理和界面。

我有类似的要求,如果停止启动服务。 我认为最简单的解决scheme是每5分钟在Windows任务计划程序中执行以下命令:

净启动MyServiceName

这个命令基本上会启动服务(如果停止),并且如果服务已经运行则不起作用。

我最近实现了一个恢复选项来运行一个PowerShell脚本,试图重新启动服务定义的次数,并在结束时发送电子邮件通知。

经过多次尝试(尽pipe我已经看到所有其他的事情)在服务中的恢复选项卡上的字段的configuration如下:

程序:Powershell.exe
**不是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 = "[email protected]" $FromAddress = "[email protected]" 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 = "[email protected]" $FromAddress = "[email protected]" 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