我不知道为什么在linux上有时候./shutdown.sh不能关闭掉tomcat,我估计是因为有其它的进程在使用它,于是每次都只能手动的kill -9去杀掉他,后来我嫌麻烦于是自己写了个监控PID的脚本,然后用JAVA程序去访问我的页面,如果异常或者超时,我就调用这个监控去杀掉tomcat,并重新启动它
        首先我要准备一个脚本叫做killtomcat.sh,哈哈,监控的原理很简单通过ps -ef|grep 来实现
 1 #!/bin/bash
#!/bin/bash
 2 export JAVA_HOME=/local/akazam/servers/java
export JAVA_HOME=/local/akazam/servers/java
 3 export CATALINA_HOME=/local/akazam/servers/tomcat
export CATALINA_HOME=/local/akazam/servers/tomcat
 4 export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/td.jar:$JAVA_HOME/jre/lib/rt.jar:$CATALINA_HOME/lib/servlet-api:$CATALINA_HOME/lib/catalina-ant.jar:$CATALINA_HOME/lib/catalina.jar:$CATALINA_HOME/lib/annotations-api.jar:$CATALINA_HOME/lib/tomcat-coyote.jar:$CATALINA_HOME/lib/tomcat-dbcp.jar:$CATALINA_HOME/lib/jsp-api.jar:$CATALINA_HOME/lib/commons-pool.jar:$CATALINA_HOME/lib/common-dbcp.jar:$CATALINA_HOME/lib/catalina-tribes:$CATALINA_HOME/lib/catalina-ha.jar:.
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/td.jar:$JAVA_HOME/jre/lib/rt.jar:$CATALINA_HOME/lib/servlet-api:$CATALINA_HOME/lib/catalina-ant.jar:$CATALINA_HOME/lib/catalina.jar:$CATALINA_HOME/lib/annotations-api.jar:$CATALINA_HOME/lib/tomcat-coyote.jar:$CATALINA_HOME/lib/tomcat-dbcp.jar:$CATALINA_HOME/lib/jsp-api.jar:$CATALINA_HOME/lib/commons-pool.jar:$CATALINA_HOME/lib/common-dbcp.jar:$CATALINA_HOME/lib/catalina-tribes:$CATALINA_HOME/lib/catalina-ha.jar:.
 5 export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$CATALINA_HOME/bin:$PATH
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$CATALINA_HOME/bin:$PATH
 6 TOMCAT_PATH=/local/akazam/servers/tomcat/bin
TOMCAT_PATH=/local/akazam/servers/tomcat/bin
 7 if [ $# -eq 0 ]
if [ $# -eq 0 ]
 8 then
    then
 9 echo "ERROR:Usage: process argument
        echo "ERROR:Usage: process argument " 1>&2
" 1>&2
10 echo "eg:If you want to restart tomcat you can enter ./restart.sh tomcat!" 1>&2
        echo "eg:If you want to restart tomcat you can enter ./restart.sh tomcat!" 1>&2
11 exit 1
        exit 1
12 fi
fi
13 path=$1
path=$1
14 set $(ps -ef|grep $path)
set $(ps -ef|grep $path)
15 pid=$2
pid=$2
16 flaggrep=$8
flaggrep=$8
17 echo "tomcat pid : $pid,flag : $flaggrep"
echo "tomcat pid : $pid,flag : $flaggrep"
18
19 if [ "$flaggrep" = "grep" -o "$flaggrep" = "/bin/bash" ]
if [ "$flaggrep" = "grep" -o "$flaggrep" = "/bin/bash" ]
20 then
then
21 echo "no thread start"
 echo "no thread start"
22 else
else
23 #kill tomcat
#kill tomcat
24 kill -9 $pid
kill -9 $pid
25 sleep 3
sleep 3
26 echo "killed thread"
echo "killed thread"
27 fi
fi
28 #start tomcat
#start tomcat
29 cd $TOMCAT_PATH
cd $TOMCAT_PATH
30 ./startup.sh
./startup.sh
31 echo "tomcat restart!"
echo "tomcat restart!"  
        接下来就要写个JAVA类来监控了
  1 package com.akazam.monitor;
package com.akazam.monitor;
  2
  3 import java.io.BufferedReader;
import java.io.BufferedReader;
  4 import java.io.File;
import java.io.File;
  5 import java.io.IOException;
import java.io.IOException;
  6 import java.io.InputStreamReader;
import java.io.InputStreamReader;
  7 import java.net.HttpURLConnection;
import java.net.HttpURLConnection;
  8 import java.net.URL;
import java.net.URL;
  9 import java.util.Date;
import java.util.Date;
 10
 11
 public class MonitorTomcat
public class MonitorTomcat  {
{
 12
 public long getLoadMS(String url)
    public long getLoadMS(String url) {
{
 13 long time1=System.currentTimeMillis();
        long time1=System.currentTimeMillis();
 14 
        
 15 int i=0;
        int i=0;
 16 // 带参数目录名方式运行 dir 命令
         // 带参数目录名方式运行 dir 命令
 17 Runtime runtime = Runtime.getRuntime();
        Runtime runtime = Runtime.getRuntime();
 18 String classDir = this.getClass().getResource("/").getPath();
        String classDir = this.getClass().getResource("/").getPath();
 19 String command= classDir+"killtomcat.sh tomcat/";
        String command= classDir+"killtomcat.sh tomcat/";
 20
 try
        try  {
{ 
 21 URL target = new URL(url);
            URL target = new URL(url); 
 22 HttpURLConnection conn = (HttpURLConnection) target.openConnection();
            HttpURLConnection conn = (HttpURLConnection) target.openConnection();  
 23 conn.setRequestMethod("GET");
            conn.setRequestMethod("GET");  
 24 conn.connect();
            conn.connect(); 
 25 BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));  
 26
 while (in.readLine() != null)
            while (in.readLine() != null) {
{
 27 i++;
                i++;
 28 }
            }  
 29
 }catch(Exception e)
        }catch(Exception e) {
{  
 30 //重启tomcat
            //重启tomcat
 31
 try
            try  {
{
 32
 Process process = runtime.exec(new String[]
                Process process = runtime.exec(new String[]  {"/bin/bash","killtomcat.sh","tomcat/"},null,new File(classDir));
{"/bin/bash","killtomcat.sh","tomcat/"},null,new File(classDir));
 33 System.out.println(e.getMessage()+"\r\nRun command is:"+command);
                System.out.println(e.getMessage()+"\r\nRun command is:"+command);
 34 if(process!=null)
                if(process!=null)
 35
 
                 {
{
 36 BufferedReader in = new BufferedReader(new InputStreamReader(
                    BufferedReader in = new BufferedReader(new InputStreamReader(
 37 process.getInputStream()));
                              process.getInputStream()));
 38 String line;
                    String line;
 39
 while ((line = in.readLine()) != null)
                    while ((line = in.readLine()) != null)  {
{
 40 System.out.println(line+"\r\n");
                        System.out.println(line+"\r\n");
 41 }
                    }
 42
 try
                     try  {
{
 43
 44
 if (process.waitFor() != 0)
                                   if (process.waitFor() != 0)  {
{
 45
 46 System.err.println("exit value = " + process.exitValue());
                                       System.err.println("exit value = " + process.exitValue());
 47
 48 }
                                   } 
 49
 50 }
                                 }
 51
 52
 catch (InterruptedException ec)
                                 catch (InterruptedException ec)  {
{  
 53
 54 System.err.println(ec);
                                    System.err.println(ec);
 55
 56 }
                                }
 57
 58
 59 
                            
 60 }
                }
 61 else return -1;
                else return -1;
 62
 } catch (IOException e1)
            } catch (IOException e1)  {
{
 63 // TODO Auto-generated catch block
                // TODO Auto-generated catch block
 64 //e1.printStackTrace();
                //e1.printStackTrace();
 65 System.out.println(e1.getMessage()+"\r\n");
                System.out.println(e1.getMessage()+"\r\n");
 66 }
            }
 67 
            
 68 }
        }  
 69 long time2 = new Date().getTime();
        long time2 = new Date().getTime();  
 70 long result = time2-time1;
        long result = time2-time1;
 71 if(result>60000)
        if(result>60000)
 72
 
         {
{
 73 //重启tomcat
            //重启tomcat
 74
 try
            try  {
{
 75 
                
 76
 Process process = runtime.exec(new String[]
                Process process = runtime.exec(new String[]  {"/bin/bash","killtomcat.sh","tomcat/"},null,new File(classDir));
{"/bin/bash","killtomcat.sh","tomcat/"},null,new File(classDir));
 77 if(process!=null)
                if(process!=null)
 78
 
                 {
{
 79 BufferedReader in = new BufferedReader(new InputStreamReader(
                    BufferedReader in = new BufferedReader(new InputStreamReader(
 80 process.getInputStream()));
                              process.getInputStream()));
 81 String line;
                            String line;
 82
 try
                            try  {
{
 83
 while ((line = in.readLine()) != null)
                                while ((line = in.readLine()) != null)  {
{
 84 System.out.println(line+"\r\n");
                                 System.out.println(line+"\r\n");
 85 }
                                }
 86
 try
                                 try  {
{
 87
 88
 if (process.waitFor() != 0)
                                       if (process.waitFor() != 0)  {
{
 89
 90 System.err.println("exit value = " + process.exitValue());
                                           System.err.println("exit value = " + process.exitValue());
 91
 92 }
                                       } 
 93
 94 }
                                     }
 95
 96
 catch (InterruptedException ec)
                                     catch (InterruptedException ec)  {
{  
 97
 98 System.err.println(ec);
                                        System.err.println(ec);
 99 System.out.println(ec.getMessage()+"\r\n");
                                        System.out.println(ec.getMessage()+"\r\n");
100 }
                                    }
101
102
 } catch (IOException e)
                            } catch (IOException e)  {
{
103 // TODO Auto-generated catch block
                                // TODO Auto-generated catch block
104 e.printStackTrace();
                                e.printStackTrace();
105 }
                            }
106 }
                }
107 else return -1;
                else return -1;
108
 } catch (IOException e)
            } catch (IOException e)  {
{
109 // TODO Auto-generated catch block
                // TODO Auto-generated catch block
110 System.out.println(e.getMessage()+"\r\n");
                  System.out.println(e.getMessage()+"\r\n");
111 }
            }
112 
        
113 }
        }
114 
    
115 
        
116 return result;
        return result;
117 }
    }
118 public static void main(String
    public static void main(String strings)
strings)
119
 
     {
{
120 if(strings.length>0)
        if(strings.length>0)
121
 
         {
{
122 String url = strings[0];
            String url = strings[0];
123 long ts = new MonitorTomcat().getLoadMS(url);
            long ts = new MonitorTomcat().getLoadMS(url);
124 if(ts==-1) System.out.println("Comman exec Error!");
            if(ts==-1) System.out.println("Comman exec Error!");
125 else System.out.println("Load Page cost :"+ts);
            else System.out.println("Load Page cost :"+ts);
126 }
        }
127 }
    }
128 }
}
129
 
