I'm not sure if this is possible but is there a way to restart the IIS through a job or a service when the server's CPU processing power is 60% and it lasts for more than 5mins? If yes, how can this be done?
Thank you!
I'm not sure if this is possible but is there a way to restart the IIS through a job or a service when the server's CPU processing power is 60% and it lasts for more than 5mins? If yes, how can this be done?
Thank you!
IIS version 6 onwards you have Application Pools and they can be Recycled based on Fixed Intervals
or Memory Based Maximums
. Based on this I dont recommend you restart IIS based on CPU usage, you may litrally be masking a High-CPU problem. Get out DebugDiag and use the High-CPU rule and take a memory dump when the CPU is over 60%.
To answer your question:
Use this code below to monitor the CPU (taken from How to get the CPU Usage in C#? - there are other examples in that thread that may be better suited to your usage, ie the one with Timer_Ticks):
PerformanceCounter cpuCounter;
PerformanceCounter ramCounter;
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
public string getCurrentCpuUsage(){
return cpuCounter.NextValue()+"%";
}
public string getAvailableRAM(){
return ramCounter.NextValue()+"MB";
}
Then after the 5 minutes you can Reset IIS:
System.Diagnostics.Process process = new System.Diagnostics.Process();
//process.StartInfo.FileName = @"C:\WINDOWS\system32\iisreset.exe";
process.StartInfo.FileName = "cmd";
process.StartInfo.Arguments = "/C iisreset /STOP";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
process.WaitForExit();
Tip: you'll need admin rights to reset IIS.