Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Monitor a running service on system
- if it's not running, it will start it automatically
- and respectively, if it stops, it will start it again after refresh rate */
- using System;
- using System.Linq;
- using System.ServiceProcess;
- namespace ServiceMonitor
- {
- class Program
- {
- public static bool ServiceExists(string ServiceName)
- {
- return ServiceController.GetServices().Any(serviceController => serviceController.ServiceName.Equals(ServiceName));
- }
- public static void Main(string[] args)
- {
- System.Console.Title = "Service monitor coded by Foxy";
- string serviceName;
- System.Console.Write("Enter the service name to monitor: ");
- serviceName = System.Console.ReadLine();
- while (true)
- {
- ServiceController sc = new ServiceController(serviceName);
- try
- {
- if (sc.Status == ServiceControllerStatus.Running)
- {
- System.Console.WriteLine("Sweet, monitoring service, if it stops I'll start it for you.");
- }
- else if (sc.Status == ServiceControllerStatus.Stopped)
- {
- System.Console.WriteLine("WARNING, service not running, restarting now");
- System.Threading.Thread.Sleep(7000);
- sc.Start();
- }
- System.Threading.Thread.Sleep(30000);
- }
- catch (Exception e) { System.Console.WriteLine("Can't find service with that name, close and try again"); Console.ReadKey(); break; };
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement