zoukankan      html  css  js  c++  java
  • 写个软件来防止服务器网站CPU百分百

    问题:

    大概每隔两个星期左右,秋色园上服务器就会来一次CPU百分百,由于问题发生的概率极低,要它重现也难,所以只能意淫是内存太少的原故。
    以前出现,远程上去结束掉进程,就正常了,悲剧的是最近秋色园VPS不知啥原因,经常远程不上去, 最后转转折折只能进VPS管理后台重启。
    要遇上CPU百分百,又是需要机缘,所以一旦发生和遇到解决的时间差度大,就会造成服务器长时间打不开,后果大伙都懂的。。。


    解决:

    方法一:设置应用池CPU策略,达到N的时候自动回收进程(不实用,排除)

    因为更新网站dll时,偶尔有顺时达到100%,可能就1-2秒,可能会导致回收到,如果再有偶尔,就会造成死循环了。


    方法二:写个软件放上去,监控cpu如果持续1分钟,直接kill掉进程。(就这么招了。。。

    花了点时间,写了下代码,扔上去了,哟省事了。。。。

    新建一个控制台。。。代码如下:

    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("监控IIS CPU w3wp 进程中,若100%,而自动结束该进程...");
                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");//读取网站的进程
                        if (procs != null && procs.Length > 0)
                        {

                            foreach (Process pro in procs)
                            {
                                if (!pro.HasExited)
                                {
                                    CheckPro(pro);
                                }
                            }
                        }
                        Thread.Sleep(TimeSpan.FromMinutes(5));//5分钟来一次。
                    }
                }
                catch (Exception err)
                {
                    Console.WriteLine(err.Message);
                }
            }
            static void CheckPro(Process pro)
            {
                int s = 0;//60秒。
                int killTimes = 0;
                //间隔时间(毫秒)
                int interval = 1000;
                //上次记录的CPU时间
                TimeSpan prevCpuTime = TimeSpan.Zero;
                while (true)
                {
                    //当前时间
                    TimeSpan curTime = pro.TotalProcessorTime;
                    //间隔时间内的CPU运行时间除以逻辑CPU数量
                    double value = (curTime - prevCpuTime).TotalMilliseconds / interval / Environment.ProcessorCount * 100;
                    prevCpuTime = curTime;

                    if (s > 0)
                    {
                        if (value > 90 && value < 100)//cpu连续超过90% 50秒就杀。
                        {
                            killTimes++;
                            if (killTimes > 50)
                            {
                                Console.WriteLine(pro.Id + " 长期高CPU,秒杀...");
                                pro.Kill();
                                Thread.Sleep(TimeSpan.FromMinutes(3));
                                return;
                            }
                        }
                        else
                        {
                            killTimes = 0;
                        }
                        if (killTimes > 0)//只有cpu超过90%才打印文字
                        {
                            Console.WriteLine(pro.Id + " CPU:" + value + " -- killtimes:" + killTimes);
                        }
                    }
                    Thread.Sleep(interval);
                    if (s > 59)
                    {
                        s = -1;
                        break;
                    }
                    else
                    {
                        s++;
                    }
                }
            }

        }
    }


    原文地址:http://www.cyqdata.com/cyq1162/article-detail-54284

    最后插播个流行漫画:


  • 相关阅读:
    LeetCode Array Easy 414. Third Maximum Number
    LeetCode Linked List Medium 2. Add Two Numbers
    LeetCode Array Easy 283. Move Zeroes
    LeetCode Array Easy 268. Missing Number
    LeetCode Array Easy 219. Contains Duplicate II
    LeetCode Array Easy 217. Contains Duplicate
    LeetCode Array Easy 189. Rotate Array
    LeetCode Array Easy169. Majority Element
    LeetCode Array Medium 11. Container With Most Water
    LeetCode Array Easy 167. Two Sum II
  • 原文地址:https://www.cnblogs.com/cyq1162/p/2804932.html
Copyright © 2011-2022 走看看