1 #!/bin/bash
2 # @Function
3 # Find out the highest cpu consumed threads of java, and print the stack of these threads.
4 #
5 # @Usage
6 # $ ./show-busy-java-threads.sh
7 #
8 # @author Jerry Lee
9
10 readonly PROG=`basename $0`
11 readonly -a COMMAND_LINE=("$0" "$@")
12
13 usage() {
14 cat <<EOF
15 Usage: ${PROG} [OPTION]...
16 Find out the highest cpu consumed threads of java, and print the stack of these threads.
17 Example: ${PROG} -c 10
18
19 Options:
20 -p, --pid find out the highest cpu consumed threads from the specifed java process,
21 default from all java process.
22 -c, --count set the thread count to show, default is 5
23 -h, --help display this help and exit
24 EOF
25 exit $1
26 }
27
28 readonly ARGS=`getopt -n "$PROG" -a -o c:p:h -l count:,pid:,help -- "$@"`
29 [ $? -ne 0 ] && usage 1
30 eval set -- "${ARGS}"
31
32 while true; do
33 case "$1" in
34 -c|--count)
35 count="$2"
36 shift 2
37 ;;
38 -p|--pid)
39 pid="$2"
40 shift 2
41 ;;
42 -h|--help)
43 usage
44 ;;
45 --)
46 shift
47 break
48 ;;
49 esac
50 done
51 count=${count:-5}
52
53 redEcho() {
54 [ -c /dev/stdout ] && {
55 # if stdout is console, turn on color output.
56 echo -ne "