Win下tomcat宕机自启vbs脚本[通俗易懂]

2022-09-15 10:36:33 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

将此下面的代码保存到一个.txt文件中,然后将后缀名命名为.vbs,再然后双击运行即可。

注意要修改的地方:

1.检查tomcat是否挂掉的访问路径,即下面代码中的:http://localhost:8080?a=” & now

2.一定要先切到bat所在目录 WshShell.CurrentDirectory=”D:Program Filesapache-tomcat-9.0.10-windows-x64apache-tomcat-9.0.10bin”,否则导致闪退

3.WshShell.Run cmd /c “& Chr(34) & shutdown & Chr(34) ,先shutdown再startup

代码语言:javascript复制
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://localhost:8080?a=" & now,false 
 
http.send       
 
if   http.Status <> 200   Then  

'一定要先切到bat所在目录
WshShell.CurrentDirectory="D:Program Filesapache-tomcat-9.0.10-windows-x64apache-tomcat-9.0.10bin"

shutdown="D:Program Filesapache-tomcat-9.0.10-windows-x64apache-tomcat-9.0.10binshutdown.bat"

WshShell.Run "cmd /c "& Chr(34) & shutdown & Chr(34)

WScript.Sleep(5000)          
 
startup="D:Program Filesapache-tomcat-9.0.10-windows-x64apache-tomcat-9.0.10binstartup.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重启记录文件.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:tomcat重启记录文件.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

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/163638.html原文链接:https://javaforall.cn

0 人点赞