locked
Restart Windows Service Automatically RRS feed

  • Question

  • Hi,

      How to restart windows service automatically each six hours?

    Tuesday, August 10, 2010 2:21 PM

Answers

  • Hi SaranRam,

     

    Welcome to MSDN Forum!

     

    We can create our own service to control the other restart.

    Like the demo following will reboot system timely. And we also can use sc command to replace the execute command to start and stop the windows service.

     

        protected override void OnStart(string[] args)
        {
          double sleeptime = ValidatorDate1(DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"), DateTime.Now.ToString("yyy-MM-dd ") + "07:00:00");
          if (sleeptime < 0)
          {
            sleeptime += 24 * 60 * 60 * 1000;
          }
          writestr("begin", sleeptime);
          System.Timers.Timer t = new System.Timers.Timer(sleeptime);
          t.Elapsed += new System.Timers.ElapsedEventHandler(rebootsystem);
          t.AutoReset = true;
          t.Enabled = true;
          writestr("end", sleeptime);
        }
        public void rebootsystem(object source, System.Timers.ElapsedEventArgs e)
        {
          System.Diagnostics.Process.Start("shutdown", "/r /f /t 0");
        }
     
        public static double ValidatorDate1(string strDateA, string strDateB)
        {
          string strFrom = strDateB;
          string strTo = strDateA;
          TimeSpan ts = Convert.ToDateTime(strFrom) - Convert.ToDateTime(strTo);
          double count = ts.TotalSeconds * 1000;
          return count;
        }
     
     
        public void writestr(string readme, double sleeptime)
        {
          //debug==================================================
          StreamWriter dout = new StreamWriter(@"c:\" + System.DateTime.Now.ToString("yyyMMddHHmmss") + readme + ".txt");
          dout.Write("\nSleepTime:" + Convert.ToString(sleeptime) + "\noperation time:" + System.DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));
          //debug==================================================
          dout.Close();
        }
    

     

    If there’s anything unclear please feel free let me know, and I am glad to help you.

     

    Regards,

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.

    • Marked as answer by SaranRam Tuesday, August 17, 2010 4:41 AM
    Monday, August 16, 2010 6:29 AM

All replies

  • A windows service should be allways running.. doing a operation or not is the diferrence.

    Why you dont use a timer and each 6h it will execute what you want.


    Best Regards, Diogo Pinheiro
    Tuesday, August 10, 2010 2:34 PM
  • Hi,

      How to restart windows service automatically each six hours?


    Hello,
    Is this any specific service or a service that you have written?

    You can use the following code to list all services. You can also use the Stop & Start methods on the scm to control the service. For an automatic start, you'll need to write your own service or a scheduled job.

     

       static void ListAllWindowServices()
        {
          /*
           * Allows you to list all windows services
           */
          ServiceController[] NonDevice_services;
          //GetServices method will retrieve non device driver services only      
          NonDevice_services = ServiceController.GetServices(); 
          //if you want to retrieve services from a particular machine the give machine_name as parameter to GetServices method
          //like :-  NonDevice_services = ServiceController.GetServices("myMachine");      
    
          Console.WriteLine("NonDevice based");
          for (int i = 0; i < NonDevice_services.Length; i++)      
          {        
            Console.WriteLine(NonDevice_services[i].ServiceName);
            Console.WriteLine(NonDevice_services[i].DisplayName);
            Console.WriteLine(NonDevice_services[i].Status);    
          }      
          //retrieve device driver services
          
          ServiceController[] device_Services;      
    
          device_Services = ServiceController.GetDevices();
          Console.WriteLine("Device based");
          for (int i = 0; i < device_Services.Length; i++)
          {
            Console.WriteLine(device_Services[i].ServiceName);
            Console.WriteLine(device_Services[i].DisplayName);
            Console.WriteLine(device_Services[i].Status);
    
          }
    
        }
    
    Tuesday, August 10, 2010 2:59 PM
  • Hi Saran,

    If this your own windows service then put timer in it to automatically restart after 6 hours.

    If its 3rd party service then you can make a .bat file with following commnad..

    sc stop [service_name]

    sc start [service_name]

    Use Task scheduler in control panel to run this .bat file after every 6 hours.

    Cheers,

     


    Mubi | http://mrmubi.com | Mark The Best Replies As Answers!
    Tuesday, August 10, 2010 3:32 PM
  • Hi SaranRam,

     

    Welcome to MSDN Forum!

     

    We can create our own service to control the other restart.

    Like the demo following will reboot system timely. And we also can use sc command to replace the execute command to start and stop the windows service.

     

        protected override void OnStart(string[] args)
        {
          double sleeptime = ValidatorDate1(DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"), DateTime.Now.ToString("yyy-MM-dd ") + "07:00:00");
          if (sleeptime < 0)
          {
            sleeptime += 24 * 60 * 60 * 1000;
          }
          writestr("begin", sleeptime);
          System.Timers.Timer t = new System.Timers.Timer(sleeptime);
          t.Elapsed += new System.Timers.ElapsedEventHandler(rebootsystem);
          t.AutoReset = true;
          t.Enabled = true;
          writestr("end", sleeptime);
        }
        public void rebootsystem(object source, System.Timers.ElapsedEventArgs e)
        {
          System.Diagnostics.Process.Start("shutdown", "/r /f /t 0");
        }
     
        public static double ValidatorDate1(string strDateA, string strDateB)
        {
          string strFrom = strDateB;
          string strTo = strDateA;
          TimeSpan ts = Convert.ToDateTime(strFrom) - Convert.ToDateTime(strTo);
          double count = ts.TotalSeconds * 1000;
          return count;
        }
     
     
        public void writestr(string readme, double sleeptime)
        {
          //debug==================================================
          StreamWriter dout = new StreamWriter(@"c:\" + System.DateTime.Now.ToString("yyyMMddHHmmss") + readme + ".txt");
          dout.Write("\nSleepTime:" + Convert.ToString(sleeptime) + "\noperation time:" + System.DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));
          //debug==================================================
          dout.Close();
        }
    

     

    If there’s anything unclear please feel free let me know, and I am glad to help you.

     

    Regards,

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.

    • Marked as answer by SaranRam Tuesday, August 17, 2010 4:41 AM
    Monday, August 16, 2010 6:29 AM
  • Thank you much, this solved my problem.
    Wednesday, June 4, 2014 3:28 PM