#!/bin/bash
hms=""
find=false
while true
do
if ps -a -T | grep "$1" > /dev/null
then
if ! $find; then
current_hour=$(date +%H)
current_minute=$(date +%M)
current_second=$(date +%S)
hms="${current_hour}${current_minute}${current_second}"
echo "Start recording the CPU usage of $1 to \"$1_cpu_usage_$hms.txt\" file..."
fi
find=true
top -H -n 1 | grep "$1" | awk '{print $10}' >> $1_cpu_usage_$hms.txt
sleep 0.5
continue
else
if $find; then
echo "The thread $1 is stop."
break
fi
echo "The thread $1 does not exist, waiting..."
sleep 1
fi
done
使用方法:cpu_record.sh thread_name