Process snapshot total time on CPU

 

This script I use to snapshot the process table and add up the times each process has on the CPU. Sometimes a process is running more than one instance, so it totals up these duplicate processes. If a process, at the time of the snapshot, has not used any CPU, it will not be shown.

if [[ -d /tmp/proctsm ]];then
rm -r /tmp/proctsm
mkdir /tmp/proctsm
else
mkdir /tmp/proctsm
fi

ps aux | sort -k3r | awk ‘{print $1,$3,$11}’ > /tmp/pstable
cat /tmp/pstable | grep -v “COMMAND” | while read user cpu process
do
procfile=/tmp/proctsm/$(echo $process | sed -e ‘s/\///g’ -e ‘s/\[//g’ -e ‘s/\]//g’ -e ‘s/\://g’ -e ‘s/\-//g’).tsm
echo $cpu >> $procfile
done

cd /tmp/proctsm
ls *.tsm | while read file
do
sum=0.0
cat $file | while read cpu
do
sum=$(echo “scale=2; ${sum}+${cpu}” | bc -l)
echo $sum >> ${file}.sum
done

done

ls *.sum | while read file
do
cpu=`tail -1 $file`
if [[ $cpu > 0 ]];then
process=${file%%.*}
echo “$process $cpu”
fi
done

About

I'm a freelance UNIX engineer working in the UK. I'm married to Tina and between us we have six children. I'm a bit of an Apple fan boy, and all the Windows machines in the house are a thing of the past now.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: