[Go] Write a software to prevent the server website CPU hundred percent

Source: Internet
Author: User
Tags server website vps

Problem:

about every two weeks or so, the server on the autumn garden will come a CPU hundred percent, because the probability of the problem occurs very low, it is difficult to reproduce it, so can only be a memory is too little of the original reason. previously appeared, the remote up to end the process, it is normal, the tragedy is the recent autumn garden VPS do not know what the reason, often remote not up, the final turn to the transition can only into the VPS management background restart. to meet the CPU hundred percent, but also need luck, so once the occurrence and the resolution of the time difference, will cause the server long time can not open, the consequences of everyone understand ...

Solve:

Method One: Set the application pool CPU policy, the automatic recycle process when n is reached (not practical, excluded)

because when updating the site DLL, occasionally there is a time to reach 100%, may be 1-2 seconds, may lead to recycling, if there are occasional, will cause a dead loop.

method Two: Write a software put up, monitor the CPU if it lasts 1 minutes, kill the process directly. (That's the trick ...) )

took a little time, write down the code, throw it up, yo easy ....

Create a new console ... The code is as follows:

Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Threading;
Using System.Diagnostics;

Namespace Iiscpuforserver
{
Class Program
{
static void Main (string[] args)
{
Console.WriteLine ("Monitor IIS CPU w3wp process, if 100%, and automatically end the process ...");
Thread thread = new Thread (new ThreadStart (Run));
Thread. IsBackground = true;
Thread. Start ();
Console.read ();
}
static void Run ()
{
Try
{
while (true)
{
process[] procs = Process.getprocessesbyname ("w3wp");//The process of reading a Web site
if (procs! = null && procs. Length > 0)
{

foreach (Process Pro in procs)
{
if (!pro. hasexited)
{
Checkpro (PRO);
}
}
}
Thread.Sleep (Timespan.fromminutes (5));//5 min to come.
}
}
catch (Exception err)
{
Console.WriteLine (Err. Message);
}
}
static void Checkpro (Process Pro)
{
int s = 0;//60 seconds.
int killtimes = 0;
Interval time (milliseconds)
int interval = 1000;
Last recorded CPU time
TimeSpan prevcputime = TimeSpan.Zero;
while (true)
{
Current time
TimeSpan curtime = Pro. Totalprocessortime;
CPU run time over interval divided by number of logical CPUs
Double value = (curtime-prevcputime). Totalmilliseconds/interval/environment.processorcount * 100;
Prevcputime = Curtime;

if (S > 0)
{
if (Value >//cpu && value < +), it kills more than 90% 50 seconds in a row.
{
killtimes++;
if (Killtimes > 50)
{
Console.WriteLine (Pro. Id + "Long-term high cpu, seconds kill ...");
Pro. Kill ();
Thread.Sleep (Timespan.fromminutes (3));
Return
}
}
Else
{
Killtimes = 0;
}
if (Killtimes > 0)//Only the CPU exceeds 90% to print the text
{
Console.WriteLine (Pro. Id + "CPU:" + value + "--killtimes:" + killtimes);
}
}
Thread.Sleep (interval);
if (S > 59)
{
s =-1;
Break
}
Else
{
s++;
}
}
}

}
}

[Go] Write a software to prevent the server website CPU hundred percent

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.