windows service

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

I want to create a windows service that will monitor another window service.
what i need for the service to do is, if a service is stopped I need it to
start the service back up

example:

service 1 - my service watches service 2 - windows service

service 2 is stopped - service 1 starts service 2

can this even be done, and if so can someone point me to an example of
creating a windows service and how to watch services?
thx
 
Mike,

You could do this in a number of ways. You could make calls through the
P/Invoke layer to OpenService, and EnumServiceStatusEx to enumerate through
all services to see if the other service is running. Another way to do this
would be to have some sort of object with the status exposed by the service.
If the object isn't there, or you can't connect, then you know the service
isn't running.

I kind of don't like the idea of having a service that runs just to run
another service. It seems somewhat wasteful, but more importantly, what do
you do if there is an error in the service that is watching the service? Do
they watch each other? You can run into some pretty nasty situations here,
IMO.

Hope this helps.
 
to monitor another service you use the
System.ServiceProcess.ServiceController's Status property.
 
We hope it would never happen, but a service could stop and thats a no no.
This service needs to be running 24/7/365. I thought it would be easier to
have it automated then have something send an email out or page someone if
the service stops. I was thinking if the service stopped then have something
start it up again, as well as read the event viewer and send a message out,
but the service would be running


Nicholas Paldino said:
Mike,

You could do this in a number of ways. You could make calls through the
P/Invoke layer to OpenService, and EnumServiceStatusEx to enumerate through
all services to see if the other service is running. Another way to do this
would be to have some sort of object with the status exposed by the service.
If the object isn't there, or you can't connect, then you know the service
isn't running.

I kind of don't like the idea of having a service that runs just to run
another service. It seems somewhat wasteful, but more importantly, what do
you do if there is an error in the service that is watching the service? Do
they watch each other? You can run into some pretty nasty situations here,
IMO.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mike said:
I want to create a windows service that will monitor another window service.
what i need for the service to do is, if a service is stopped I need it to
start the service back up

example:

service 1 - my service watches service 2 - windows service

service 2 is stopped - service 1 starts service 2

can this even be done, and if so can someone point me to an example of
creating a windows service and how to watch services?
thx
 
Mike,

Just an opinion, but I think a better strategy would be to have a
notification go out when it is detected that a service is no longer running.
The reason for this is that if the service keeps running and crashing, you
might do more damage (depending on what the service does, maybe it inserts
data somewhere, or something of that nature) by keeping it running than just
running it over and over again.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mike said:
We hope it would never happen, but a service could stop and thats a no no.
This service needs to be running 24/7/365. I thought it would be easier to
have it automated then have something send an email out or page someone if
the service stops. I was thinking if the service stopped then have something
start it up again, as well as read the event viewer and send a message out,
but the service would be running


message news:#Rqb#[email protected]...
Mike,

You could do this in a number of ways. You could make calls through the
P/Invoke layer to OpenService, and EnumServiceStatusEx to enumerate through
all services to see if the other service is running. Another way to do this
would be to have some sort of object with the status exposed by the service.
If the object isn't there, or you can't connect, then you know the service
isn't running.

I kind of don't like the idea of having a service that runs just to run
another service. It seems somewhat wasteful, but more importantly, what do
you do if there is an error in the service that is watching the service? Do
they watch each other? You can run into some pretty nasty situations here,
IMO.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mike said:
I want to create a windows service that will monitor another window service.
what i need for the service to do is, if a service is stopped I need
it
 
Hi Mike,

In the service property you can select several options in the Recovery tab,
you can run a program or restart the service.

You could select run a program and that is the one that send out the
email/page/etc

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Mike said:
We hope it would never happen, but a service could stop and thats a no no.
This service needs to be running 24/7/365. I thought it would be easier to
have it automated then have something send an email out or page someone if
the service stops. I was thinking if the service stopped then have something
start it up again, as well as read the event viewer and send a message out,
but the service would be running


message news:#Rqb#[email protected]...
Mike,

You could do this in a number of ways. You could make calls through the
P/Invoke layer to OpenService, and EnumServiceStatusEx to enumerate through
all services to see if the other service is running. Another way to do this
would be to have some sort of object with the status exposed by the service.
If the object isn't there, or you can't connect, then you know the service
isn't running.

I kind of don't like the idea of having a service that runs just to run
another service. It seems somewhat wasteful, but more importantly, what do
you do if there is an error in the service that is watching the service? Do
they watch each other? You can run into some pretty nasty situations here,
IMO.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mike said:
I want to create a windows service that will monitor another window service.
what i need for the service to do is, if a service is stopped I need
it
 
We have that in place, for this service. What happens is if the service is
hit, sits idle for a period of time, he has to be restarted,and right a now
a person restarts it and we're find and dandy. But i would like to do this
automatically. If the service stops due to a failure of something then no i
don't want to restart the service and a alert should be sent out.

Its a strange vendor product that uses this service


Nicholas Paldino said:
Mike,

Just an opinion, but I think a better strategy would be to have a
notification go out when it is detected that a service is no longer running.
The reason for this is that if the service keeps running and crashing, you
might do more damage (depending on what the service does, maybe it inserts
data somewhere, or something of that nature) by keeping it running than just
running it over and over again.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mike said:
We hope it would never happen, but a service could stop and thats a no no.
This service needs to be running 24/7/365. I thought it would be easier to
have it automated then have something send an email out or page someone if
the service stops. I was thinking if the service stopped then have something
start it up again, as well as read the event viewer and send a message out,
but the service would be running


message news:#Rqb#[email protected]...
Mike,

You could do this in a number of ways. You could make calls
through
the
P/Invoke layer to OpenService, and EnumServiceStatusEx to enumerate through
all services to see if the other service is running. Another way to
do
this
would be to have some sort of object with the status exposed by the service.
If the object isn't there, or you can't connect, then you know the service
isn't running.

I kind of don't like the idea of having a service that runs just
to
run
another service. It seems somewhat wasteful, but more importantly,
what
do
you do if there is an error in the service that is watching the
service?
Do
they watch each other? You can run into some pretty nasty situations here,
IMO.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I want to create a windows service that will monitor another window
service.
what i need for the service to do is, if a service is stopped I need
it
to
start the service back up

example:

service 1 - my service watches service 2 - windows service

service 2 is stopped - service 1 starts service 2

can this even be done, and if so can someone point me to an example of
creating a windows service and how to watch services?
thx
 
No need to create a Windows service for this, shedule a simple console
application to start a system boot.
Next console sample uses System.Management and WMI to monitor Windows
service events and take appropriate actions (here restart spooler service).

// This code demonstrates how to monitor the Win32_Spooler events

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Management;
class WMIEvent {
public static void Main() {
WMIEvent we = new WMIEvent();
ManagementEventWatcher w= null;
WqlEventQuery q;
ManagementOperationObserver observer = new ManagementOperationObserver();

// Bind to local machine
ManagementScope scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true; //sets required privilege
try {
q = new WqlEventQuery();
q.EventClassName = "__InstanceOperationEvent";
q.Condition = @"TargetInstance ISA 'Win32_Service' and
TargetInstance.Name='Spooler'";
q.WithinInterval = new TimeSpan(0,0,10); // poll interval 10 secs.
w = new ManagementEventWatcher(scope, q);
w.EventArrived += new EventArrivedEventHandler(we.ServiceEventArrived);
w.Start();
Console.ReadLine(); // Press enter to terminate
w.Stop();
}
catch(Exception e) {
Console.WriteLine(e);
}
}

public void ServiceEventArrived(object sender, EventArrivedEventArgs e) {
ManagementBaseObject mbo =
(ManagementBaseObject)e.NewEvent["TargetInstance"];
if((bool)mbo["Started"] == false)
{
Console.WriteLine("Service:{0} stopped", mbo["DisplayName"]);
RestartService(mbo["Name"].ToString());
}
}

public void RestartService(string ServiceName)
{
ManagementPath myPath = new ManagementPath();
myPath.Server = System.Environment.MachineName;
myPath.NamespacePath = @"root\CIMV2";
myPath.RelativePath = "Win32_Service.Name='" + ServiceName + "'";
using (ManagementObject nac= new ManagementObject(myPath))
{

ManagementBaseObject outParams = nac.InvokeMethod("StartService", null,
null);
uint ret = (uint)(outParams.Properties["ReturnValue"].Value);
if(ret == 0)
Console.WriteLine("Service Restart Succeeded");
else Console.WriteLine("Failed with error code: {0}", ret);
}
}

}


Willy.
 
Back
Top