Results 1 to 1 of 1

Thread: Process Restarter

  1. #1

    Thread Starter
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Process Restarter

    A common quick/temporary fix for server administrators is to check if software has crashed and restart it.

    So Here's a simple console application that will do that for you. It accepts 2-3 paramaters

    Usage:
    ProcessRestarter <processName> <restartcommand/exe location> [interval-ms]

    Example:
    ProcessRestarter failware C:\servers\failware.exe 30000

    The above example will check if the process 'failware' is running every 30 seconds, and if not, will start a new process at 'C:\servers\failware.exe' (so now. no need for human intervention to restart the failware app if it crashes, just check the restarters console window to see if a crash has occured)

    the interval is optional and will default to 60,000ms (60 seconds)

    Code:
    using System;
    using System.Diagnostics;
    
    namespace ProcessRestarter
    	
    {
    	class Program
    	{
    		public static void Main(string[] args)
    		{
    			Console.WriteLine("Process Restarter is booting...");
    			string processName = "";
    			string restartCommand = "";
    			int interval = 60000;
    			
    			if(args.Length >= 2) {
    				processName = args[0];
    				restartCommand = args[1];
    				if(args.Length >= 3) int.TryParse(args[2],out interval);
    				Console.WriteLine("Scanning for Process name: " + processName);
    				Console.WriteLine("If not found, will send system command: " + restartCommand);
    				Console.WriteLine("Scan will occur every " + interval.ToString() + "ms");
    				while(true) {
    					if(Process.GetProcessesByName(processName).Length == 0) {
    						Console.WriteLine("[" + DateTime.Now.ToString("dd/MM/yyyy") + " " + DateTime.Now.ToShortTimeString() + "] Process not found, sending restart command");
    						Process.Start(restartCommand);
    						System.Threading.Thread.Sleep(2000);
    					}
    						
    					System.Threading.Thread.Sleep(interval);
    				}
    			} else {
    				Console.WriteLine("Not enough parameters specified.\nUsage .exe <processname> <restartcommand/exe location> [interval-ms]\nscan interval is optional and defaults to 60000 ms");
    				Console.Write("Press any key to continue . . . ");
    				Console.ReadKey(true);
    			}
    			
    			
    			
    		}
    	}
    }
    Last edited by Phill64; Oct 28th, 2009 at 04:50 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width