批处理脚本开发常用命令

文章目录

常用脚本命令

  • 打印
echo success rename publish-copy to publish

错误打印

// 它会将 success rename publish - 写入文件 publish_20231001,而非输出到控制台。
echo success rename publish -  > publish_20231001

打印当前目录

echo current directory: %cd%
  • 暂停,按任意键继续 pause &REM press any key to continue
  • 强制等待2秒。加上“/nobreak”不能被中断 timeout /t 2 /nobreak
  • 获取当前日期,中英文系统都能用,输出格式是 yyyyMMdd
for /f "tokens=2 delims==" %%G in ('wmic os get localdatetime /value ^| findstr "."') do set datetime=%%G
set "backup_suffix=!datetime:~0,8!"
  • 重命名文件夹
 ren "publish" "publish_!backup_suffix!"
  • 复制粘贴文件夹
robocopy "publish" "publish-copy" /E /COPYALL /DCOPY:T /R:3 /W:1 /NP /NFL /NDL
  • 切换到指定目录
set "DEPLOY_DIR=D:\publish"
cd /d   "%DEPLOY_DIR%"
  • 启动/停止IIS 上的某个站点
set "SITE_NAME=WebApi"
// 启动
%systemroot%\system32\inetsrv\appcmd.exe start site %SITE_NAME%
//  停止
%systemroot%\system32\inetsrv\appcmd.exe stop site %SITE_NAME%
  • 退出 exit /b 1
    exit 主命令,表示退出当前环境(脚本或子程序)。
    /b 参数,表示仅退出当前批处理脚本或子程序(b 代表 “batch”)。
    1 退出代码(Exit Code),通常 0 表示成功,非零值(如 1)表示失败。

  • 逻辑判断

    • 检查错误码是否 ​​等于 200​​ !errorlevel! equ 200
    • 检查错误码是否 ​​不等于 0​​ !errorlevel! neq 0
  • jenkins里面执行cmd脚本,如果脚本 包含 timeout /t 2 /nobreak(延时2秒),可能会报错 ERROR: Input redirection is not supported, exiting the process immediately.
    解决办法:ping -n 2 127.0.0.1 >nul (发送两次ping命令,两次之间间隔1s,发送2次,就是延时1秒)

  • 使用curl发送post命令,成功则继续执行下一个步骤,不成功则循环发送,最多发送n次,

REM ----------[stop Service ,max MAX_RETRY]----------
:retry_stop_service
echo try to stop service(!retry_count!/%MAX_RETRY_COUNT% retry)...

REM ----------[stop Service ]----------
REM ----- Call curl to stop service -----
set "curl_output="
set "http_code="
for /f "delims=" %%a in ('
    curl -X POST ^
         -H "Content-Type: application/json" ^
         -d "{}" ^
         --max-time %SHUTDOWN_TIMEOUT% ^
         --silent ^
         --show-error ^
         --write-out "###CODE=%%{http_code}###" ^
         http://%ip%:%SERVICE_PORT%/openapi/actuator/shutdown ^
         2^>^&1
') do set "curl_output=!curl_output!%%a"

REM ----- resolve http code-----
set "http_code=500"
for /f "tokens=3 delims==#" %%j in ('echo !curl_output! ^| findstr /r "###CODE=[0-9][0-9][0-9]###"') do (
    set "http_code=%%j"
)

ping -n 2 127.0.0.1 >nul  rem need to wait  等待,可能服务还没有停止

REM ----- curl result -----
if "!http_code!"=="200" (
    echo  stop service successfully  ^| output:!curl_output!
    goto :service_stopped
) else (
    echo [Error] fail to stop service ^| response code :!http_code! ^| output:!curl_output!
)

REM ----- retry -----
retry_count 小于 MAX_RETRY_COUNT 则继续执行
if !retry_count! lss %MAX_RETRY_COUNT% (
    set /a "retry_count+=1"
    echo wait 5 seconds and retry...
    ping -n 6 127.0.0.1 >nul
    goto :retry_stop_service
) else (
    echo [Critical] Reached max retry count of %MAX_RETRY_COUNT%!
     exit /b 1
)

:service_stopped
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值