服务器的IIS经常自动停机,每次都要我手动上去重启服务,查了许多相关贴子都说是Access的JET引擎导致IIS停机,我这里论坛用的是DVBBS免费的Access版,才懒的去改SQL版呢。本来前两天下了个"WEB服务器运行状况监控V1.3.3",本指望它能帮点忙,也省的自己动手,不过它重启的方法有问题,加上有个选项“重启IIS(失败则重启电脑)”,使得服务器一天重启N多次。一怒之下又自己上了。

此方法同样可适用Tomcat等服务,只要指定页面和服务名即可。也可以用于Web端重启服务器,不过因该不好用,除非你让Tomcat的启动账号有Administrator的权限。

												import java.io.InputStream;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;
import java.util.Date;/**
 * <p>Title:IIS服务监护 </p>
 * <p>Description: </p>
 * <p>Copyright: flashman.com.cn Copyright (c) 2005</p>
 * <p>Company: flashman.com.cn</p>
 * @Jeff zhu
 * @version 1.0
 */public class IISMonitor extends Thread{//服务操作成功标识//  private static final String SUCCESS_SIGN = "successfully";//英文系统privatestatic final String SUCCESS_SIGN ="成功";//中文系统//页面检测间隔时间privatestatic final int REFRESH_TIMER = 5 * 60 * 1000;//页面活动时间privatestatic final int MONITOR_TIMER = 10 * 1000;

  Runtime runtime;public IISMonitor(){
    runtime = Runtime.getRuntime();}/**
   * 启动服务
   * @param name 服务名
   * @return
   */public boolean startService(String name){return execute("net start "+ name).indexOf(SUCCESS_SIGN)>= 0;}/**
   * 停止服务
   * @param name 服务名
   * @return
   */public boolean stopService(String name){return execute("net stop "+ name +" /y").indexOf(SUCCESS_SIGN)>= 0;}/**
   * 页面是否活动
   * @param link
   * @return
   */public boolean pageIsAlive(String link){

    boolean result =true;

    PageCheck page =new PageCheck(link);
    page.start();

    long now =System.currentTimeMillis();
    try {while(!page.alive){this.sleep(1000);if(System.currentTimeMillis()- now > MONITOR_TIMER){
          result =false;break;}//        System.out.println(System.currentTimeMillis() - now);}}
    catch (InterruptedException ex){
      ex.printStackTrace();
      result =false;}

    page.stop();return result;}/**
   * 执行命令行
   * @param cmd
   * @return 控制台信息
   */privateString execute(String cmd){String line, message ="";

    try {

      Process process = Runtime.getRuntime().exec(cmd);

      try {
        BufferedReader readIn =new BufferedReader(new InputStreamReader(process.getInputStream()));while((line = readIn.readLine())!=null){
          message += line +"\n";}
        readIn.close();} catch (IOException ex){
        ex.printStackTrace();}}catch (IOException ex){
      message ="";}//System.out.println(message);return message;}/**
   * 启动方法
   * @param args
   */publicstaticvoid main(String[] args){new Thread(){publicvoid run(){

        IISMonitor monitor =new IISMonitor();while(true){
          try {String link, message ="";System.out.print(newDate().toLocaleString()+" checked...");
            link ="http://localhost/iisMonitor.asp";if(monitor.pageIsAlive(link)){System.out.print("the page is Alive!\n");}else{System.out.print("the page is Dead!!!\n");//StopIISSystem.out.print("Stoped IIS...");if(monitor.stopService("iisadmin")){System.out.print("successfully.\n");}else{System.out.print("unsuccessful!!!\n");}//StartIISSystem.out.print("Started IIS...");if(monitor.startService("W3SVC")){System.out.print("successfully.\n");}else{System.out.print("unsuccessful!!!\n");}}System.out.println(message);this.sleep(IISMonitor.REFRESH_TIMER);}
          catch (Exception ex){
            ex.printStackTrace();}}}}.start();}}/**
 * 页面检测类
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
class PageCheck extends Thread{public boolean alive;privateString link;public PageCheck(String link){this.alive =false;this.link = link;}publicvoid run(){

    try {
      URLConnection urlConnection;String line, result ="";

      urlConnection =new URL(link).openConnection();
      BufferedReader readIn =new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));while((line = readIn.readLine())!=null){
        result += line +"\n";}
      readIn.close();
      alive =true;}
    catch (Exception ex){
      ex.printStackTrace();
      alive =false;}}}

 

下面这段存成iisMonitor.asp放在服务器上

<%
Response.Buffer=false
Response.Write now

'假死测试'For I=1 To 1000000' Response.Write I & "<br>"'Next%>