Skip to content

Instantly share code, notes, and snippets.

@hikoma
Created March 7, 2012 14:52
Show Gist options
  • Save hikoma/1993616 to your computer and use it in GitHub Desktop.
Save hikoma/1993616 to your computer and use it in GitHub Desktop.
Prints java stack traces of java threads eating the CPU
#!/bin/bash
threshold=${1-95}
now=$(date '+%Y-%m-%d %H:%M:%S')
cache=()
jps -q | xargs ps hH k -pcpu o pid,lwp,pcpu,args \
| awk "\$3 >= $threshold" | while read line; do
array=($(echo "$line"))
pid=${array[0]}
lwp=$(printf '%#x' ${array[1]})
if [ -z "${cache[$pid]}" ]; then
cache[$pid]=$(jstack $pid)
fi
echo "[$now] ${line}"
echo
echo "${cache[$pid]}" | sed -n "/nid=$lwp/,/^$/p"
echo
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment