在工作中,有时候JBoss服务器当机,需要使用命令行去获取JSTACK和JSTAT日志以便分析原因。但获取上述日志需要先查询进程号,在生产上因为时间问题,基本上不被允许。
因此写了一个批处理命令来完成这个动作。
限制:
只能有一个java.exe进程在运行
必须将java_home\bin目录添加到path环境变量
@echo off&setlocal enabledelayedexpansion REM ====================== set /a cnt=10 REM ====================== set /a j=0,k=16*1024*1024,p=0 set dt=%date:~,4%%date:~5,2%%date:~8,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6,2% for /f "skip=1 tokens=1-2 delims= " %%a in ('wmic process where "name='java.exe'" get WorkingSetSize^,Handle') do ( if %%b LSS !k! ( REM echo "%%b less than %k%" ) else ( set /a p=%%a set /a j=j+1 ) ) if %p% EQU 0 ( echo Cannot find Java Process memory use greater than 16M. ) else if NOT %j% EQU 1 ( echo Find too more Java Process, cannot determine which one. ) else ( echo Saving JBoss thread status to c:... jstack -l %p% > "C:\JSTACK_%dt%.log" echo Saving JBoss memory status to c:... jstat -gc -t %p% 2000 %cnt% > "C:\JSTAT_%dt%.log" ) echo Finished! pause
时间: 2024-10-04 03:50:16