通过shell 控制 arthas 生成火焰图

梦康 2022-12-06 12:58:17 678

#!/bin/bash
export TERM=xterm
ctime=$(date "+%H-%M-%S")
cpu_percent=`top -bn 1|grep Cpu|awk -F " " '{print int($2)}'`

echo "${ctime} CPU:${cpu_percent}%"

if [ ${cpu_percent} -gt 70 ] ; then
  echo "${ctime} 开始采集最占 cpu 的线程"
  mknod arthas_input p
  exec 8<> arthas_input
  ./as.sh <&8 &
  echo -e "1\n" >> arthas_input

  echo "profiler start" >> arthas_input
  sleep 40
  echo "profiler stop --format html" >> arthas_input
  echo "quit" >> arthas_input
  rm -f arthas_input
  sleep 2s
fi

然后再加个定时任务

* * * * * /bin/bash /home/admin/zmk.arthas.sh >> /home/admin/zmk.arthas.log 2>&1