将此下面的代码保存到一个.txt文件中,然后将后缀名命名为.vbs,再然后双击运行即可。
注意要修改的地方:
1.检查tomcat是否挂掉的访问路径,即下面代码中的:http://paidan.mrifx.cn?a=" & now
2.一定要先切到bat所在目录 WshShell.CurrentDirectory="tomcat路径bin/即可",否则导致闪退
代码:
https://blog.csdn.net/iceylin/article/details/81109630
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 51 52 53 54 55 56 | 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://paidan.mrfix.cn?a=" & now, false http.send if http.Status <> 200 Then '一定要先切到bat所在目录 WshShell.CurrentDirectory="D:\apache-tomcat-diaodu- 7.0 . 50 \bin\" shutdown= "D:\apache-tomcat-diaodu-7.0.50\bin\shutdown.bat" WshShell.Run "cmd /c " & Chr( 34 ) & shutdown & Chr( 34 ) WScript.Sleep( 5000 ) startup= "D:\apache-tomcat-diaodu-7.0.50\bin\startup.bat" WshShell.Run "cmd /c" & Chr( 34 ) & startup & Chr( 34 ) if (fso.fileexists( "C:\tomcat重启记录文件.txt" )) then '打开文件,参数 1 为forreading, 2 为forwriting, 8 为appending set file=fso.opentextfile( "C:\tomcat_log.txt" , 8 , true ) file.writeline "tomcat:" file.writeline now file.writeline "chong xin qi dong" ts.writeblanklines 2 file.close else '创建文件,参数 1 为forreading, 2 为forwriting, 8 为appending set file=fso.createtextfile( "C:\tomcat_log.txt" , 2 ,ture) '写入文件内容,有三种方法:write(x)写入x个字符,writeline写入换行,writeblanklines(n)写入n个空行 file.writeline "tomcat:" file.writeline now file.writeline "chong xin qi dong" file.writeblanklines 2 file.close end if end if WScript.Sleep( 300000 ) loop |
这些运行好了,就直接运行运行这个vbs脚本,只要服务器宕机,它就能自动重启tomcat.并记录日志。(由于是中文名,导致创建文件乱码,所以建议在fso.createtextfile( "C:\tomcat重启记录文件.txt",2,ture)里用英文字母。)
![image.png](http://www.wurao.xin/static/userImages/20180729/1532878143648079186.png)
关闭vbs代码:cmd.exe /C taskkill.exe /F /IM "wscript.exe" & taskkill.exe /F /IM "cscript.exe"
本站博客:www.wurao.xin