Skip to content

Instantly share code, notes, and snippets.

@bsenduran
Last active May 11, 2024 23:04
Show Gist options
  • Save bsenduran/02e8bf024fcaaa7707a6bb2321e097a8 to your computer and use it in GitHub Desktop.
Save bsenduran/02e8bf024fcaaa7707a6bb2321e097a8 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ "$#" -ne 3 ]; then
echo "usage: sh thread-analyze.sh <pid> <number-of-dumps> <interval>"
exit
fi
count=$2
for i in `seq 1 $count`;
do
jstack -l $1 > thread_dump_`date "+%F-%T"`.txt &
ps --pid $1 -Lo pid,tid,%cpu,time,nlwp,c > thread_usage_`date "+%F-%T"`.txt &
if [ $i -ne $count ]; then
echo "sleeping for $3 [$i]"
sleep $3
fi
done
@bsenduran
Copy link
Author

bsenduran commented Oct 24, 2016

Taking 4 thread dumps in one minute interval for java process with pid 1234
sh thread-analyze.sh 1234 4 1m

Available time units

  • s for seconds (the default)
  • m for minutes.
  • h for hours.
  • d for days.

@sumsum77
Copy link

sumsum77 commented Mar 8, 2024

To prevent colons (:) in filenames it would be better to replace "+%F-%T" (resulting in 2024-03-08-10:26:28) for filenames with "+%Y%m%d-%H%M%S" (resulting in 20240308-102616)...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment