一个监测IIS,并定时重新启动的程序。

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Net;

namespace WuyinIISControler
{
 public class MainSrv : System.ServiceProcess.ServiceBase
 {
  private System.Timers.Timer timer1;
  ///
  /// 必需的设计器变量。
  ///
  private System.ComponentModel.Container components = null;
  string url =  "http://www.5inet.net/checkIIS.asp";
  int timeout = 6000;
  int repeatTime = 300000;
  EventLog log =null;
  int Times = 1;
  
  public MainSrv()
  {
   // 该调用是 Windows.Forms 组件设计器所必需的。
   InitializeComponent();

// TODO: 在 InitComponent 调用后添加任何初始化
  }

// 进程的主入口点
  static void Main()
  {
   System.ServiceProcess.ServiceBase[] ServicesToRun;
 
   // 同一进程中可以运行多个用户服务。若要将
   //另一个服务添加到此进程,请更改下行
   // 以创建另一个服务对象。例如,
   //
   //   ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
   //
   ServicesToRun = new System.ServiceProcess.ServiceBase[] { new MainSrv() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
  }

///
  /// 设计器支持所需的方法 - 不要使用代码编辑器
  /// 修改此方法的内容。
  ///
  private void InitializeComponent()
  {
   this.timer1 = new System.Timers.Timer();
   ((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
   //
   // timer1
   //
   this.timer1.Interval = 300000;
   this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
   //
   // MainSrv
   //
   this.ServiceName = "WuyinIISControler";
   ((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();

}

///
  /// 清理所有正在使用的资源。
  ///
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

///
  /// 设置具体的操作,以便服务可以执行它的工作。
  ///
  protected override void OnStart(string[] args)
  {
   log = new EventLog();
   log.Log = "Application";
   log.Source = this.ServiceName;
   foreach(string arg in args)
   {
    if(arg.ToUpper().StartsWith("/URL:"))
     this.url = arg.Split(Convert.ToChar(":"))[1]+":"+arg.Split(Convert.ToChar(":"))[2];
    if(arg.ToUpper().StartsWith("/TIMEOUT:"))
     this.timeout = int.Parse(arg.Split(Convert.ToChar(":"))[1].ToString())*1000;
    if(arg.ToUpper().StartsWith("/REPEATTIME:"))
     this.repeatTime = int.Parse(arg.Split(Convert.ToChar(":"))[1].ToString())*1000*60;

}
   // TODO: 在此处添加代码以启动服务。
   this.timer1.Interval = this.repeatTime;
   this.timer1.Enabled=true;
   if(!CheckIIS(this.url))
    this.RestartIIS();
  }

private bool CheckIIS(string url)
  {
   bool b = false;
   System.Net.WebResponse wsp = null;
   string msg = "正在执行第" + this.Times.ToString() + "次检测:\n检测地址:" + url + "\n超时设置:" + (this.timeout/1000).ToString() + "秒\n检测间隔时间:" + (repeatTime/1000/60).ToString() + "分\n检测结果:";
   try
   {
    System.Net.WebRequest wq = WebRequest.Create(url);
    wq.Timeout = this.timeout;
    wsp = wq.GetResponse();
    if(wsp.Headers.Count>0)
    {
     b = true;
     msg+="正常";
    }
    wsp.Close();
   }
   catch(Exception ex)
   {
    b = false;
    msg+="错误\n详细内容:" + ex.Message;
   }
   finally
   {
     log.WriteEntry(msg);
   }
   return b;
  }

private void RestartIIS()
  {
   try
   {
    System.Diagnostics.Process.Start("iisreset.exe","/restart");
    log.WriteEntry("正在执行重新启动命令:iisreset.exe /restart");
   }
   catch(Exception ex)
   {
    log.WriteEntry("发生错误:\n" + ex.ToString());
   }
  }
 
  ///
  /// 停止此服务。
  ///
  protected override void OnStop()
  {
   // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
   this.timer1.Enabled=false;
   log.Close();
   log.Dispose();
  }

private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  {
   this.Times++;
   if(!CheckIIS(this.url))
    this.RestartIIS();
  }
 }
}

===============================================================================

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;

namespace WuyinIISControler
{
 ///
 /// ProjectInstaller 的摘要说明。
 ///
 [RunInstaller(true)]
 public class ProjectInstaller : System.Configuration.Install.Installer
 {
  private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
  private System.ServiceProcess.ServiceInstaller serviceInstaller1;
  ///
  /// 必需的设计器变量。
  ///
  private System.ComponentModel.Container components = null;

public ProjectInstaller()
  {
   // 该调用是设计器所必需的。
   InitializeComponent();

// TODO: 在 InitializeComponent 调用后添加任何初始化
  }

///
  /// 清理所有正在使用的资源。
  ///
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }
  #region 组件设计器生成的代码
  ///
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  ///
  private void InitializeComponent()
  {
   this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
   this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
   //
   // serviceProcessInstaller1
   //
   this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
   this.serviceProcessInstaller1.Password = null;
   this.serviceProcessInstaller1.Username = null;
   //
   // serviceInstaller1
   //
   this.serviceInstaller1.DisplayName = "无垠IIS控制器";
   this.serviceInstaller1.ServiceName = "WuyinIISControler";
   this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
   this.serviceInstaller1.AfterInstall+=new InstallEventHandler(serviceInstaller1_AfterInstall);
   //
   // ProjectInstaller
   //
   this.Installers.AddRange(new System.Configuration.Install.Installer[] {
                       this.serviceProcessInstaller1,
                       this.serviceInstaller1});

}
  #endregion

private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
  {
   System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController("WuyinIISControler");
   sc.Start(new string[]{"/url:http://www.5inet.net/checkIIS.asp","/timeout:10","/repeatTime:5"});
   sc.Dispose();
  }
 }
}

posted on 2004-02-24 01:06 嘻哈呵嘿 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/skyover/archive/2004/02/24/1550.html

一个监测IIS,并定时重新启动的程序。相关推荐

  1. 微信小程序:后台数据库与云数据库对比取最后一个值并且取用定时更新

    微信小程序:后台数据库与云数据库对比取最后一个值并且取用定时更新的方式来现在在前端网页上 我们有时候会遇到这样的问题,在后台数据库提取到数据后想要提取他们中的某些有共同特征的一些数据,这时候我们就可以 ...

  2. iis服务器 关闭自动启动,设置IIS服务器定时自动重启的方法

    最近,有一朋友的IIS服务器老是出现问题,运行一段时间下来就会出现访问服务器上的网站时提示数据库连接出错,然后重启IIS后网站又能正常访问了,实在找不出是什么原因导致了这个问题.不过最终我想到了一个笨 ...

  3. 编写一个Windows服务程序,定时从数据库中拿出记录发送邮件

    前言:编写一个Windows服务程序,定时从数据库中拿出记录发送邮件. 测试环境:Visual Studio 2005 SP1.Windows Server 2003 SP2 一.新建项目 打开VS2 ...

  4. java应用重启导致数据丢失_java – 在重新启动应用程序后从SharedPreferences恢复时设置丢失数据...

    我在android上使用SharedPreference来存储一组字符串.根据我的知识存储和检索它,但是当重新启动应用程序时,一些数据会丢失.字符串是逐个添加的,在添加它之前我检索集合,添加字符串然后 ...

  5. Spring Boot和Thymeleaf:重新加载模板和静态资源,而无需重新启动应用程序

    Thymeleaf是围绕自然模板的概念设计的,该模板允许进行静态原型制作:模板逻辑不会影响用作原型的模板. 尽管这是一项很棒的技术,但您可能还希望在运行的Spring Boot应用程序中查看结果,而不 ...

  6. 定时执行java程序_如何让Java程序定时运行

    由于项目开发的需要,必须实现让一个Java程 序定时运行.比如,我的项目中,有一个网络蜘蛛,需要从互联网上抓取数据,与其配合,有另一个程序来对新抓取的页面进行索引的创建,由于数据源更新频率不 高,我们 ...

  7. android 重新启动应用程序,在AsyncTask完成后重新启动完整的Android应用程序

    我编码的应用程序检查/ sdcard下的目录中是否有特殊的ZIP文件,如果没有,则开始下载并解压缩.即使是子目录,下载和解压缩也可以工作.但是我需要在完成后重新启动应用程序 – 这不起作用. 起初我有 ...

  8. python 重启程序_重新启动Python程序

    我会绕过所有的焦虑,你可能会从试图重新运行自己,把它交给环境. 我的意思是:当程序以特定的"重新启动"代码退出时,有一个控制程序只在循环中运行程序(使用与给定参数相同的参数).这可 ...

  9. 定时打开指定程序软件

    由于最近工作中经常用到定时执行一些程序,以前都是用计划任务,但是想想何不自己开发一个定时执行程序的小软件呢,用了一晚上的功夫开发完成,功能比较简单.可以定时执行多个程序.可以支持开机自启动.设定好之后 ...

最新文章

  1. 【web安全】Xss Exploits and Defense翻译4
  2. HTML中各种位置距离关系
  3. nginx多站点配置,以及隐藏index.php
  4. PDF 格式优化的一点经验
  5. Windows 任务管理器中的几个内存概念
  6. Leetcode —— 886. 可能的二分法
  7. h3c trunk口改access_H3C交换机二层应用及三层交换基本配置
  8. 29muduo_net库源码分析(五)
  9. 栈和队列基本概念,顺序栈的表示和实现
  10. docker安装nessus方法
  11. php 多关键字搜索,php 多关键字搜索示例
  12. java office 集成开发_[转载]Java集成PageOffice在线打开编辑word文件 - Spring Boot
  13. 【0173】推荐6款最好使用的PostgreSQL GUI工具
  14. matlab结果输出到文本的方法
  15. Launcher 的启动
  16. Windows下如何配置tomcat环境变量
  17. es6知识总结 模块 承诺加载
  18. Linux进程间通信源码剖析,共享内存(shmget函数详解)
  19. 基于matlab的单周期控制三相高功率因数并网逆变器的建模与仿真,基于Matlab的单周期控制三相高功率因数并网逆变器的建模与仿真...
  20. 第十二届蓝桥杯(2021年)模拟赛 Python组(第一期) 题目+个人解答

热门文章

  1. Android -ui控件
  2. Stack Overflow 2016 最新架构探秘
  3. kafka学习(一)初识kafka
  4. 合作开发工具——freeze和pipreqs
  5. 2017-12-04HTML布局_div布局
  6. 常对象成员和常成员函数
  7. 昂靠的由来[本博作者爆料]
  8. Spring+ehcache缓存实例
  9. 今天学会了如何察看SSDT里面的东西、修改里面的地址
  10. android代码设置全屏