public void RestartService(string serviceName, TimeSpan timeout) { CmdHelper cmd = new CmdHelper(); CommandExecutionResult result = cmd.ExecuteCommand2( @"cmd /c sc \\{0} stop {1}".FormatWith(this.machine, serviceName), this.userName, this.password, this.Domain); if (result.ExitCode == 0) { DateTime start = DateTime.Now; while (!this.GetServiceStatus(serviceName).Equals("Stopped", StringComparison.OrdinalIgnoreCase)) { if (DateTime.Now.Subtract(start) < timeout) { System.Threading.Thread.Sleep(2000); } else { throw new Exception("Timeout after {0} when stopping service {1} on machine {2}.".FormatWith(timeout.ToString(), serviceName, this.machine)); } } CommandExecutionResult resultStart = cmd.ExecuteCommand2( "cmd /c sc \\{0} start {1}".FormatWith(this.machine, serviceName), this.userName, this.password, this.Domain); if (result.ExitCode == 0) { start = DateTime.Now; while (!this.GetServiceStatus(serviceName).Equals("running", StringComparison.OrdinalIgnoreCase)) { if (DateTime.Now.Subtract(start) < timeout) { System.Threading.Thread.Sleep(2000); } else { throw new Exception("Timeout after {0} when starting service {1} on machine {2}.".FormatWith(timeout.ToString(), serviceName, this.machine)); } } } else { throw new Exception("Failed to start service {0} on machine {1} after stopped it.\r\n{2}".FormatWith(serviceName, this.machine, resultStart.ToSerializedXmlString())); } } else { throw new Exception("Failed to stop service {0} on machine {1}.\r\n{2}".FormatWith(serviceName, this.machine, result.ToSerializedXmlString())); } }
public void RestartServices(string[] services, TimeSpan timeoutPerService) { // Stop them one by one (Must one by one because they may have dependencies) CmdHelper cmd = new CmdHelper(); for (int i = 0; i < services.Length; i++) { Log.Info("Restart services with {0}\\{1}...", this.Domain, this.userName); CommandExecutionResult result = cmd.ExecuteCommand2( @"cmd /c sc \\{0} stop {1}".FormatWith(this.machine, services[i]) //, this.userName //, this.password //, this.Domain ); if (result.ExitCode == 0) { DateTime start = DateTime.Now; string status = this.GetServiceStatus(services[i]); while (!status.Equals("Stopped", StringComparison.OrdinalIgnoreCase)) { if (DateTime.Now.Subtract(start) < timeoutPerService) { Log.Info("Waiting..."); System.Threading.Thread.Sleep(2000); } else { throw new Exception("Timeout after {0} when stopping service {1} on machine {2}. Latest status: {3}.".FormatWith(timeoutPerService.ToString(), services[i], this.machine, status)); } status = this.GetServiceStatus(services[i]); } } else { Log.Info(result.StandardOutput.ToString()); Log.Error(result.StandardError.ToString()); DateTime start = DateTime.Now; string status = this.GetServiceStatus(services[i]); while (!status.Equals("Stopped", StringComparison.OrdinalIgnoreCase)) { if (DateTime.Now.Subtract(start) < timeoutPerService) { Log.Info("Waiting..."); System.Threading.Thread.Sleep(2000); } else { throw new Exception("Timeout after {0} when stopping service {1} on machine {2}. Latest status is {3}.".FormatWith(timeoutPerService.ToString(), services[i], this.machine, status)); } status = this.GetServiceStatus(services[i]); } //throw new Exception("Failed to stop service {0} on machine {1}.\r\n{2}".FormatWith(services[i], this.machine, result.ToSerializedXmlString())); } } // Start them one by one for (int i = services.Length - 1; i >= 0; i--) { CommandExecutionResult result = cmd.ExecuteCommand2( @"cmd /c sc \\{0} start {1}".FormatWith(this.machine, services[i]) //, this.userName //, this.password //, this.Domain ); if (result.ExitCode == 0) { DateTime start = DateTime.Now; string status = this.GetServiceStatus(services[i]); while (!status.Equals("running", StringComparison.OrdinalIgnoreCase)) { if (DateTime.Now.Subtract(start) < timeoutPerService) { Log.Info("Waiting..."); System.Threading.Thread.Sleep(2000); } else { throw new Exception("Timeout after {0} when starting service {1} on machine {2}. Latest status: {3}.".FormatWith(timeoutPerService.ToString(), services[i], this.machine, status)); } status = this.GetServiceStatus(services[i]); } } else { Log.Info(result.StandardOutput.ToString()); Log.Error(result.StandardError.ToString()); DateTime start = DateTime.Now; string status = this.GetServiceStatus(services[i]); while (!status.Equals("running", StringComparison.OrdinalIgnoreCase)) { if (DateTime.Now.Subtract(start) < timeoutPerService) { Log.Info("Waiting..."); System.Threading.Thread.Sleep(2000); } else { throw new Exception("Timeout after {0} when starting service {1} on machine {2}. Latest status: {3}.".FormatWith(timeoutPerService.ToString(), services[i], this.machine, status)); } status = this.GetServiceStatus(services[i]); } //throw new Exception("Failed to start service {0} on machine {1}.\r\n{2}".FormatWith(services[i], this.machine, result.ToSerializedXmlString())); } } }