这个脚本的问题在于时间判断部分和终止进程部分的语法错误。以下是修复后的脚本:

@echo off
setlocal

set "port=8989"

rem 获取当前时间
for /f "tokens=1-4 delims=:." %%a in ("%time%") do (
    set "hour=%%a"
    set "minute=%%b"
    set "second=%%c"
)

rem 如果时间为凌晨1点,则继续执行
if "%hour%"=="01" (
    rem 检查端口号是否被占用
    netstat -ano | findstr "%port%" >nul
    if %errorlevel% equ 0 (
        rem 获取进程ID
        for /f "tokens=5" %%i in ('netstat -ano ^| findstr "%port%"') do set "pid=%%i"

        rem 终止进程
        taskkill /f /pid "%pid%"
        echo Terminated process with PID %pid% listening on port %port%.
    ) else (
        echo Port %port% is not in use.
    )

    rem 延迟1秒
    timeout /t 1 >nul
) else (
    rem 延迟1分钟
    timeout /t 60 >nul
)

endlocal

修复的地方包括:

  1. if %errorlevel% == 0改为if %errorlevel% equ 0,使用equ进行相等比较。
  2. 去除最后一行的多余空格
echo offsetlocalset port=8989rem 获取当前时间for f tokens=1-4 delims= a in time do set hour=a set minute=b set second=crem 如果时间为凌晨1点则继续执行if hour==01 rem 检查端口号是否被占用 netstat -ano findstr por

原文地址: https://www.cveoy.top/t/topic/iI7h 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录