惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
WordPress大学
WordPress大学
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 聂微东
L
LangChain Blog
云风的 BLOG
云风的 BLOG
Jina AI
Jina AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
InfoQ
大猫的无限游戏
大猫的无限游戏
MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
小众软件
小众软件
量子位
The GitHub Blog
The GitHub Blog
博客园 - 【当耐特】

博客园 - 步青云

comfyui一次成功的抽卡 在 MySQL 5.7 中实现每月手动创建分区 tp5设置log文件输出 微信小程序连接websocket随记 命令提示符 在win64上使用bypy进行百度网盘文件上传 phpqrcode生成任意尺寸的二维码 Visual Studio 2017 安装心得 用火车头采集器8.6免费版采集图片 为了android sdk下载,必须修改hosts win2003服务器网站和数据库的二周自动备份 ultraedit去空行 用Navicat for MySQL进行数据库的导入导出 尝试jquery插件的开发 移动硬盘数据惊魂恢复记 织梦内容管理系统 4_0_1 的rand()优化 FlashDevelop的第一个程序 RHEL6下编译Mplayer Linux下第一个C程序 用SciTE调试C++程序遇到的小问题
用C#的控制台程序监控apache网站是否正常
步青云 · 2012-01-16 · via 博客园 - 步青云

自己随便写的,没用在真实环境上。仅供参考。


添加一个应用

选中 System.ServiceProcess

下面是源代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.ServiceProcess;
using System.Threading;

namespace frey
{
class Program
{
static void Main(string[] args)
{
string pageUrl = "http://localhost/";

bool istrue = false;

while (true)
{
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(pageUrl);
HttpWebResponse myHttpWebResponse; //=无法连接到远程服务器
try
{
myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
/*
HttpStatusCode.OK 等效于 HTTP 状态 200。OK 指示请求成功,且请求的信息包含在响应中。这是最常接收的状态代码。
HttpStatusCode.BadGateway 等效于 HTTP 状态 502。BadGateway 指示中间代理服务器从另一代理或原始服务器接收到错误响应。
HttpStatusCode.NotFound 等效于 HTTP 状态 404。NotFound 指示请求的资源不在服务器上。
*/
if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
{
istrue = true;
}
else
{
istrue = false;
}
myHttpWebResponse.Close();
}
catch (WebException ex)
{
istrue = false;
Console.WriteLine(ex.Status); //ConnectFailure
}
//判断网站是否正常
Console.WriteLine(istrue.ToString());
//不正常重启APACHE服务
if (istrue == false)
{
ServiceController sc = new ServiceController();
sc.ServiceName = "apache2.2";
if (sc.CanStop)
{
if (sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
Console.WriteLine("停止apahe服务器");
}
}
Console.WriteLine(sc.Status);
if (sc.Status == ServiceControllerStatus.Stopped)
{
try
{
sc.Start();
Console.WriteLine("时间:{0}", DateTime.Now.ToString());//时间
Console.WriteLine("启动apahe服务器");
}
catch { }
}
sc.Close();
}
Thread.Sleep(6000);
Console.WriteLine("6秒"); //每6秒检测一次,实际环境可设为60秒或更多
}
}
}
}

希望大家给出意见。

自己感觉不足的地方,一是超时没有判断。二是对apache的假死还没想到合适的办法。