问题描述

在Windows服务器上部署Tomcat服务时,用Tomcat的启动脚本启动服务(start.bat)。手动双击执行时服务正常重启,但通过Windows计划任务调度时始终失效。任务历史记录显示"操作成功完成",但实际服务状态未变化,日志文件也未生成。

Tomcat启动bat脚本,其中加了一行打印启动日志

1
echo %DATE% %TIME% Tomcat start begin. >> "%CATALINA_HOME%\startLog.log"
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
57
58
59
60
61
62
63
@echo off
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements. See the NOTICE file distributed with
rem this work for additional information regarding copyright ownership.
rem The ASF licenses this file to You under the Apache License, Version 2.0
rem (the "License"); you may not use this file except in compliance with
rem the License. You may obtain a copy of the License at
rem
rem http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.

rem ---------------------------------------------------------------------------
rem Start script for the CATALINA Server
rem ---------------------------------------------------------------------------

setlocal

rem Guess CATALINA_HOME if not defined
set "CURRENT_DIR=%cd%"

rem 打印脚本是否成功启动
echo %DATE% %TIME% Tomcat start begin. >> "%CATALINA_HOME%\startLog.log"

if not "%CATALINA_HOME%" == "" goto gotHome
set "CATALINA_HOME=%CURRENT_DIR%"
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
cd ..
set "CATALINA_HOME=%cd%"
cd "%CURRENT_DIR%"
:gotHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
echo The CATALINA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
goto end
:okHome

set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"

rem Check that target executable exists
if exist "%EXECUTABLE%" goto okExec
echo Cannot find "%EXECUTABLE%"
echo This file is needed to run this program
goto end
:okExec

rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgs

call "%EXECUTABLE%" start %CMD_LINE_ARGS%

:end

计划任务里看定时启动成功,可以看到上次执行时间

image-20250411104937976

但没有日志生成,.log文件里没有内容

初级排查

  1. 最小化测试验证
    创建基础测试脚本test.bat

    1
    2
    @echo off
    echo %DATE% %TIME% >> D:\test.log

    ✅ 计划任务成功写入日志
    ❌ 原服务控制脚本仍然失败

  2. 日志增强改造
    在原脚本关键节点插入诊断日志:

    1
    2
    echo [DEBUG] 当前目录:%CD% >> debug.log
    echo [DEBUG] 原始路径:%CATALINA_HOME% >> debug.log

问题分析

  1. 差异对比分析

    执行方式 当前目录 日志输出内容
    手动执行 D:\tomcat\bin CATALINA_HOME正确识别
    计划任务 C:\Windows\System32 CATALINA_HOME路径错误
  2. 关键代码段解析

    1
    2
    3
    set "CURRENT_DIR=%cd%"
    if not "%CATALINA_HOME%" == "" goto gotHome
    set "CATALINA_HOME=%CURRENT_DIR%" # 依赖当前目录的致命问题

    原因总结:脚本中获取了环境的相对路径,当执行目录变为System32时,路径拼接%CATALINA_HOME%\bin\catalina.bat指向了不存在的C:\Windows\System32\bin\catalina.bat

解决方案

方案1:配置计划任务起始目录

  1. 打开任务属性 → 操作 → 编辑

  2. 在"起始于(可选)"填入脚本真实路径D:\tomcat\bin

    image-20250411110540043

方案2:脚本绝对路径改造

1
2
3
- set "CATALINA_HOME=%CURRENT_DIR%"
+ set "CATALINA_HOME=D:\tomcat"
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome

方案3:动态路径计算

1
2
3
set "SCRIPT_DIR=%~dp0"
set "CATALINA_HOME=%SCRIPT_DIR%.."
echo 计算后的路径:%CATALINA_HOME% >> debug.log