1

I have a cluster of legacy windows service. All of this services is installed on several servers, but the service may be down for some reasons, such as memory leak, filesystem exceptions and sql connection exceptions. So we have to remote the server and check the status of the service daily. Although restore action is added. But it is still not robust, controllable.

The solution I want to find is, integrate all of those exist services to one windows service . This host service can run the sub services and scheduled them and monitor them, when errors occurred, my host service can restore , log the error to database and send email to related people. The solution also can make me to schedule the service, such as stop , set periodically. So I want to know is there a way I don't need to modify the assembly of the exist windows service which inherit from servicebase and integrate them to my framework. I thought that the OS can host the sub service , there must a way that my Host service or frame work can also host the sub service and schedule them too.

My final target is there is only one windows service , it is my host service. Other sub service runs under my service without installed to the OS.

I have dug some related thread below, but they can't fit my requirements:

Community
  • 1
  • 1
Scott Lei
  • 28
  • 5
  • http://en.wikipedia.org/wiki/Svchost – leppie Mar 27 '13 at 06:37
  • 1
    @leppie from what I've read, you can't use svchost for your own services, http://stackoverflow.com/questions/849613/can-i-use-svchost-exe-to-host-my-own-services. Scott, Can you give us more information about the services you want to host together? Are they all written in .NET? Do you have the source code for them? – Despertar Mar 27 '13 at 06:42
  • @Despertar. The windows services is written in .Net. I have source code of them. But it is too much, and some of them may existed before i became a programmer. :) So, i don't want to touch the source code unless i have to. – Scott Lei Mar 27 '13 at 08:37

2 Answers2

0

When you say services do you mean proper services or executables converted into services? If it is the latter, you've got full control over it. You could spawn the subservices as threads or even processes. The big problem is killing off the processes. With every new release of Windows OS, they add more and more security features which makes it difficult to kill off processes.

Threads may be a problem in that if one thread dies due to memory leaks, it affects the program. The whole program may have to kill itself off and restart.

A note of warning: services are run as a user called "Local User". You cannot login as "Local User" and even as the top administrator (called Administrator), you can't kill off "Local User" services or processes. Only the "Local User" can kill them off.

Have you had a look at shareware products like firedaemon? They do something similar.

cup
  • 7,589
  • 4
  • 19
  • 42
  • Many thanks for your reply. The sub services what i have mentioned are proper services. Actually, Except this sub windows services, i also have dozens of windows scheduled task. These scheduled tasks run executable applications. These executable have been added to my Service Framework. I run them in single thread, but the the thread wrapped as a "ExceptionSafeThread", one thread crash will not stop other thread. So the memmory leak problem can be avoid. The last question you mentioned is the permission to stop the thread, my design is to let the Host service("Local User") to stop them. – Scott Lei Mar 27 '13 at 08:24
0

So i want to know is there a way i don't need to modify the assembly of the exist windows service which inherit from servicebase and integrate them to my framework

It sounds like you don't want to modify existing code, so I would say by far the simplest and most effective solution would be to write a separate service which monitors the other ones.

The ServiceController class, contains all the functionallity you would need to check the status of other services without resorting to sc or another utiltities calls.

You can maintain a .txt file or an .xml file with a list of the services you want to monitor.

string[] servicesToWatch = GetServiceList();
foreach (string service in servicesToWatch)
{
    try
    {
        ServiceController sc = new ServiceController(service);
        if (sc.Status != ServiceStatus.Running)
        {
            sc.Start();
            sc.WaitForStatus(ServiceStatus.Running, TimeSpan.FromMinutes(2));
            // log to database that service was restarted
        }
    }
    catch (Exception e)
    {
        // log to database that service could not be started
        // send emails to necessary people
    }
}

You can then put the above code in a timer or in a while loop that checks every x minutes.

Despertar
  • 21,627
  • 11
  • 81
  • 79
  • Many thanks for your reply. Currently , my Service Framewrok can monitor the exist windows service. It can restart the service which failed ,it can also send a mail to related peple. What i used to monitor the service status is ServiceController as you suggested. But it is not enought. Next step ,we need to uninstall all of the existing Windows service and integrate to my Service Framework. which we can do fully control. – Scott Lei Mar 27 '13 at 08:29