重启iis线程池和iis站点_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 重启iis线程池和iis站点

重启iis线程池和iis站点

 2013/12/5 15:26:03  [↑起↑]  博客园  我要评论(0)
  • 摘要:服务器监控.一定时间内或者iis异常,就重启线程池和站点一般重启站点没啥用..重启线程池效果明显.重启站点:///<summary>///根据名字重启站点.(没重启线程池)///</summary>///<paramname="sitename"></param>staticvoidRestartWEbSite(stringsitename){try{varserver=newServerManager();varsite=server
  • 标签:iiS 重启 线程

服务器监控. 一定时间内或者iis异常,就重启线程池和站点

一般重启站点没啥用..

重启线程池 效果明显.

 

重启站点:

class="csharpcode">   /// <summary>
        /// 根据名字重启站点.(没重启线程池)
        /// </summary>
        /// <param name="sitename"></param>
        static void RestartWEbSite(string sitename)
        {
            try
            {
                var server = new ServerManager();
                var site = server.Sites.FirstOrDefault(s => s.Name == sitename);
                if (site != null)
                { 
                    site.Stop();
                    if (site.State == ObjectState.Stopped)
                    { 
                    }
                    else
                    {
                        Console.WriteLine("Could not stop website!");
                        throw new InvalidOperationException("Could not stop website!");
                    } 
                    site.Start();
                }
                else
                {
                    Console.WriteLine("Could not find website!");

                    throw new InvalidOperationException("Could not find website!");
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        /// <summary>
        /// 重启完之后.要再检测下.是否开启
        /// </summary>
        /// <param name="sitename"></param>
        static void FixWebsite(string sitename)
        {
            try
            {
                var server = new ServerManager();
                var site = server.Sites.FirstOrDefault(s => s.Name == sitename);
                if (site != null)
                {
                    if (site.State != ObjectState.Started)
                    {
                        Thread.Sleep(500);

                        //防止状态为正在开启
                        if (site.State != ObjectState.Started)
                        {
                            site.Start(); 
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

重启iis线程池:

    /// <summary>
        /// 线程池名字
        /// </summary>
        /// <param name="name"></param>
        static void RestartIISPool(string name)
        {
            string[] cmds = { "c:", @"cd %windir%\system32\inetsrv", string.Format("appcmd stop apppool /apppool.name:{0}", name), string.Format("appcmd start apppool /apppool.name:{0}", name) }; 
            Cmd(cmds);
            CloseProcess("cmd.exe");
        }

        /// <summary>
        /// 运行CMD命令
        /// </summary>
        /// <param name="cmd">命令</param>
        /// <returns></returns>
        public static string Cmd(string[] cmd)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.AutoFlush = true;
            for (int i = 0; i < cmd.Length; i++)
            {
                p.StandardInput.WriteLine(cmd[i]);
            }
            p.StandardInput.WriteLine("exit");

            string strRst = p.StandardOutput.ReadToEnd();
            
            //Debug.Print(strRst);

            p.WaitForExit();
            p.Close();
            return strRst;
        }

        /// <summary>
        /// 关闭进程
        /// </summary>
        /// <param name="ProcName">进程名称</param>
        /// <returns></returns>
        public static bool CloseProcess(string ProcName)
        {
            bool result = false;
            var procList = new ArrayList();
            foreach (Process thisProc in Process.GetProcesses())
            {
                var tempName = thisProc.ToString();
                int begpos = tempName.IndexOf("(") + 1;
                int endpos = tempName.IndexOf(")");
                tempName = tempName.Substring(begpos, endpos - begpos);
                procList.Add(tempName);
                if (tempName == ProcName)
                {
                    if (!thisProc.CloseMainWindow())
                        thisProc.Kill(); // 当发送关闭窗口命令无效时强行结束进程
                    result = true;
                }
            }
            return result;
        }

 

 

原文连接:http://abujj.me/archives/706

发表评论
用户名: 匿名