I would like to occasionally, via a shell script, take java thread dumps and append them to a file while also allowing them to go to catalina.out which is where FD 1 is pointed to for the Java process. I tried kill -3 $(pidof java) |tee -a /dumpfile but ended up with an empty file because Java's STDOUT is not my script's controlling terminal. Is there any way to do this without closing the Java process and reopening it with its output redirected? Linux with bash shell.

有帮助吗?

解决方案 2

Here is the solution I ended up using to capture and email the thread dumps. It doesnt redirect STDOUT of the process, it just grabs the pertinent info from catalina.out.

#!/bin/bash
CATOUT=/opt/tomcat/logs/catalina.out
DUMPSTART=$(grep -n dump|tail -n1|cut -d':' -f1)
DUMPEND=$(wc -l $CATOUT|cut -d' ' -f1)

kill -3 $(pidof java)
sed -n "${DUMPSTART},${DUMPEND)p" $CATOUT |mail -s "Java thead dump" myemail@foo.com 

其他提示

Try this then:

1- in on terminal:

tail -f /proc/$(pidof java)/fd/1 | tee -a /dumpfile

2- in another terminal:

kill -3 $(pidof java)

This assumes you have read permissions on /proc/java_pid/fd/1 which should be file if you're root.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top