Here is a solution that ultimately helped me get stack traces from an unprepared Java process running as a different user in a terminal session or as a Windows service.
jstack.cmd :
@for /f "usebackq tokens=2" %%I in ('tasklist /NH /FI "imagename eq tomcat.exe" /FI "username eq SYSTEM"') DO set PID=%%I @for /f "tokens=2 delims==." %%G in ('wmic os get localdatetime /value') do @set datetime=%%G @set date_time=%datetime:~0,4%_%datetime:~4,2%_%datetime:~6,2%__%datetime:~8,2%_%datetime:~10,2%_%datetime:~12,2% PsExec.exe /accepteula -h -s -d cmd /c "%JAVA_HOME%\jstack.exe" -l %PID% ^>%~dp0jstack_%date_time%.txt
The first line identifies the PID by process name and user.
Then PsExec with adopted EULA helps to call jstack in elevated mode with properly escaped output redirection in the jstack*.txt dir directory with the time stamp included in the file name .
You may need to add the PsExec and JDK directories to your PATH in advance.
Earlier, I tried to force a thread stream using SendSignal, but it never worked reliably with Windows service processes.
Vadzim Dec 08 '17 at 23:02 on 2017-12-08 23:02
source share