You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows故障转移集群通用脚本无法重启PrTomcat服务问题求助

问题根因
  • 集群资源脚本超时被强制终止:Windows故障转移集群自定义资源的默认上线超时时间为30~60秒,你脚本中单次等待休眠就有15秒,当Tomcat停止耗时超过集群超时阈值时,集群会直接杀死脚本进程,因此仅能看到第一次打印的Stop Pending日志,后续停止等待、启动服务的逻辑完全未执行。
  • 停止等待的时间粒度过大:StopService中每次休眠15秒,没有设置最大等待时长限制,若服务出现卡死无法停止的情况,会无限等待直到被集群终止,没有异常兜底逻辑。
  • 启动逻辑存在死循环隐患:StartService中判断服务是否处于Stop Pending的循环内,没有重新获取最新的服务状态对象,服务实际停止后,内存中存储的旧对象状态仍为Stop Pending,会永久卡在循环中无法继续执行。
  • 全局错误吞服导致异常无感知:脚本开头开启了On Error Resume Next,所有WMI调用、服务操作的运行时错误都会被直接忽略,哪怕中间操作失败也不会留下报错日志,无法定位具体错误点。
修复方案
  1. 调整集群资源超时配置
    打开故障转移集群管理器,找到对应的自定义资源,将「资源上线超时时间」调整为大于Tomcat最长停止耗时,比如设置为200秒。

  2. 优化服务停止等待逻辑
    缩小等待间隔到1~2秒,增加最大等待时长限制,超过阈值后打印错误日志退出,避免无限等待,参考修改代码:

Sub StopService(strComputer, strServiceName)
  Dim cimv2, oService, Result, waitCount, maxWait
  maxWait = 160 ' 最大等待160秒,覆盖最长150秒的停止耗时
  waitCount = 0
  ' 原有WMI对象获取、停止前校验逻辑不变...
  Result  = oService.StopService
  If 0 <> Result Then
    Resource.LogInformation "Stop " & strServiceName & " error: " & Result
    exit Sub 
  End If 
  
  Do While oService.Started And waitCount < maxWait
    Set oService = cimv2.Get("Win32_Service.Name='" & strServiceName & "'")
    Resource.LogInformation now & " StopService PrTomcat " & oService.State
    Wscript.Sleep 1000 ' 每次只等1秒,提升状态检测灵敏度
    waitCount = waitCount + 1
  Loop
  If waitCount >= maxWait Then
    Resource.LogInformation "Stop " & strServiceName & " timeout after 160s"
    ' 可根据需求增加强制杀死Tomcat进程的兜底逻辑
  End If
End Sub
  1. 修复启动逻辑的状态刷新问题
    在Stop Pending判断循环中每次都获取最新服务状态:
Sub StartService(strComputer, strServiceName)
  Dim cimv2, oService, Result
  ' 原有WMI对象获取逻辑不变...
  Set oService = cimv2.Get("Win32_Service.Name='" & strServiceName & "'")
  Do While StrComp(oService.State,"Stop Pending",1) = 0
    Wscript.Sleep 1000
    Resource.LogInformation "The service " & strServiceName & " Stop Pending."
    Set oService = cimv2.Get("Win32_Service.Name='" & strServiceName & "'") ' 新增刷新服务状态
  Loop
  ' 后续服务启动、启动状态等待逻辑不变...
End Sub
  1. 优化错误捕获逻辑
    要么移除全局的On Error Resume Next,要么在每个关键操作后判断Err.Number是否不为0,打印错误信息后退出,避免错误被静默吞服。

内容的提问来源于stack exchange,提问作者Autin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 12:54:09