I have a Windows service that exits unexpectedly every few days. Is there a simple way to monitor it to make sure it gets restarted quickly if it crashes?
8 Answers
Under the Services application, select the properties of the service in question.
View the recovery tab - there are all sorts of options - I'd set First & Second Failure to Restart the Service, Third to run a batch program that BLAT's out an email with the third failure notification.
You should also set the Reset Fail Count to 1 to reset the fail count daily.
EDIT:
Looks like you can do this via a command line:
SC failure w3svc reset= 432000 actions= restart/30000/restart/60000/run/60000
SC failure w3svc command= "MyBatchFile.cmd"
Your MyBatchFile.CMD file can look like this:
blat - -body "Service W3svc Failed" -subject "SERVICE ERROR" -to Notify@Example.com -server SMTP.Example.com -f Administrator@Example.com
- 3,647
- 21
- 27
-
4All good stuff, but +1 especially for the email notification advice and the command-line stuff. – Maximus Minimus Jul 31 '09 at 18:27
-
1Where should the batch file live on disk? – Matt Jan 02 '15 at 19:38
-
1Anywhere. With `SC failure w3svc command= "MyBatchFile.cmd"` it should be in the path or C:\Windows\System32. You can put it in any directory if you use a full path i.e. `SC failure w3svc command= "c:\Stuff\MyBatchFile.cmd"` – Christopher_G_Lewis Jan 02 '15 at 22:19
-
Chrissy LeMaire has a script that wraps this in powershell: https://blog.netnerds.net/2020/05/windows-services-recovery-for-sql-server/ for multiple servers. – Christopher_G_Lewis May 21 '20 at 18:22
Open Services.msc, double-click on the service to open the Properties of the service, there is a Recovery tab and those settings should allow you to restart the service upon failure.
- 201
- 2
- 5
Try to set recover time to zero:
The command line equivalent:
SC failure YOUR_SERVICE_NAME reset= 0 actions= restart/0/restart/0/restart/0
Anyway sometimes auto recover doesn't work correctly and it recommended to use third party software. It seems when service exit gracefully with exit code of 0 windows don't try to recover it.
I had similar requirement to start a service if stopped. The simplest solution I thought was to execute the below command in windows task scheduler every 5 minutes:
net start MyServiceName
This command will basically start the service (if stopped) and has no effect if the service is already running.
- 21
- 1
I am using ServiceKeeper on my windows 2008 server at HostForLife.eu and it works very good. Previously, I had a review on ServiceHawk, but I prefer to use ServiceKeeper for its easier management and interface.
I recently implemented a recovery option to run a powershell script that attempts to restart the service a defined number of times and sends an email notification at the conclusion, it also .
After several attempts (and despite all the other things I have seen) The configuration of fields on the recovery tab in services is as follows:
Program: Powershell.exe
**Not C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe
Command line parameters: -command "& {SomePath\YourScript.ps1 '$args[0]' '$args[1]' '$args[n]'}"
eg: -command "& {C:\PowershellScripts\ServicesRecovery.ps1 'Service Name'}"
**The $args are parameters that will be passed to your script. These are not required.
here is the powershell script:
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
- 11
- 1
This was my answer on a similar thread Hope this helps...
You can schedule a simple vbs script like this one to restart periodically the service on the computer if needed.
strComputer = "." strSvcName = "YOUR_SERVICE_NAME" set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set objService = objWMI.Get("Win32_Service.Name='" & strSvcName & "'") If objService.State= "Stopped" Then objService.StartService() End If
Someone asked a similar question over at Super User: You could install a tool that monitors windows services. Something like Service Hawk would help you keep the services started, or allow you to schedule automatic restarts (possibly during the night) to keep the service running smoothly.