如果你使用的是Windows系统,并且想让tomcat挂掉之后自动重启的话,可以直接复制下面的代码,然后保存到一个文本文件中,然后将后缀名命名为.vbs,再然后双击运行即可。如:tomcat自动重启脚本.vbs
这里稍微解释一下,几点值得注意的内容:

1.检查tomcat是否挂掉的访问路径,即下面代码中的:http://xxxxx(指定一个项目的访问路径,可以是网站首页等)?a=” & now。为什么后面要加一个参数a呢?而参数的值为now呢?在vbs中,now是代表当今时间的一个变量,而我们每一次去访问那个路径,检查tomcat是否挂掉的时候,带上一个参数,而参数的值为当前的时间,就可以避免缓存问题,达到每一次访问的路径都是一个新的路径。

2.WshShell.Run(“startup.bat”) 是执行一个叫startup的bat文件,其实就是执行一个Windows的批处理,而我这个脚本是放在tomcat的startup.bat文件同级目录的,所以这里直接写WshShell.Run(“startup.bat”) 就可以了,你可以根据自己的需求,改一下里面的路径即可。
以下是脚本的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
on   error   resume   Next       

Dim a

a = True

set WshShell = WScript.CreateObject("WScript.Shell")

set fso=createobject("scripting.filesystemobject")

Do While a

set http = CreateObject("Microsoft.XMLHTTP")

http.open "GET","http://xxxxx(指定一个项目的访问路径,可以是网站首页等)?a=" & now,false

http.send

if http.Status <> 200 Then

WshShell.Run("shutdown.bat")

WScript.Sleep(10000)

WshShell.Run("startup.bat")

if (fso.fileexists("C:\Users\iteam\Desktop\tomcat重启记录文件.txt")) then
'打开文件,参数1为forreading,2为forwriting,8为appending
set file=fso.opentextfile("C:\xxxx.txt",8,true)
file.writeline "tomcat在"
file.writeline now
file.writeline "自动重启了一次"
ts.writeblanklines 2
file.close
else
'创建文件,参数1为forreading,2为forwriting,8为appending
set file=fso.createtextfile( "C:\xxxx.txt",2,ture)

'写入文件内容,有三种方法:write(x)写入x个字符,writeline写入换行,writeblanklines(n)写入n个空行
file.writeline "tomcat在"
file.writeline now
file.writeline "自动重启了一次"
file.writeblanklines 2
file.close
end if
end if
WScript.Sleep(30000)

loop