日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/4/15 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一个监测IIS,并定时重新启动的程序。 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一個監測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,并定时重新启动的程序。的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。