10

We have a service running on a Windows Server 2003 machine. This service watches a particular folder on an FTP server, and when files appear there, it invokes one of a few different executables to process them.

I've been asked to find a way for staff to be alerted in some way when this service hangs or stops.

Can anyone suggest anything with just this much information? If not, what else would you need to know?

Seems we could write ANOTHER service to watch THIS service, but then there's a chance THAT one would stop ... so we haven't resolved anything.

user477526
  • 155
  • 2
  • 2
  • 10

3 Answers3

6

About the only thing that I know if is writing another application or service that monitors if that service is running; something like that shouldn't have any unexpected behavior and stop, hopefully.

Another thing to do is go to the service in Windows, go to its properties, and then go to recovery options. From here, you can set the behavior of a service if it is to fail. The options in Windows 7 are to restart the service or computer, or run a program. This program could send some sort of notification. However, I don't know if any or all of these options exist in Server 2003. This would also not likely work if the service were to just hang, but a service watching it probably wouldn't either.

Also, if you have the source code, you can override some of the service-related methods such as OnStop() (for C#) to send a notification, but I don't believe this works with a failure.

My personal choice would be to set the recovery options just to restart the service on failure, unless it repeatedly fails, which there is also an option for. But just do what you think will work best for you; there isn't really a fail-safe method to do it.

UPDATE: I did check, and Server 2003 does indeed have the same recovery options in the service manager. As the guys said above, you can deal with that, but it is only in C++ from what I have seen; there is also a command prompt way to do it:

sc failure [servicename] reset= 0 actions= restart/60000

I found that command here and you can look at it more in its MSDN documentation. You could call this command from C# or other languages if you are not using C++, or use it directly from the command prompt if you do not have the source code.

Community
  • 1
  • 1
Daniel Underwood
  • 2,191
  • 2
  • 22
  • 48
  • I looked at the Properties for the service, and 'Take No Action' was specified on the Recovery tab. I changed it to 'Restart Service'. I'm going to go with this method for the time being, as it seems logical and was the simplest. – user477526 Jul 31 '13 at 18:20
  • That's the way I would go. I updated the answer with some automated ways to do it if you need to change it on multiple machines or deploy it this way. – Daniel Underwood Jul 31 '13 at 18:21
3

Use ChangeServiceConfig2() to define Failure Actions for your service. You could use that to invoke an external command to issue the alert (or do pretty much anything else you want) if the service terminates unexpectedly.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

The SCM (the component which handles services) has built-in auto-restart logic that you can take advantage of to restart your service, as necessary. Additionally and/or alternatively, you can configure 'custom actions' to be associated with a failure of the service - the custom action can include launching a program of your own, which could then log the failure, and perhaps manually restart your service.

You can read more about such custom actions on MSDN by looking at the documentation of the structure used to configure such actions: SERVICE_FAILURE_ACTIONS. Once you fill that structure, you notify the SCM by calling the ChangeServiceConfig2 function.

Please don't ask "well, what happens if my failure handler program crashes" :)

Nik Bougalis
  • 10,495
  • 1
  • 21
  • 37