This shell script monitors and calculates CPU usages for one hour:
#!/bin/bash
#Name: pcpu_usage.sh
#Description: Script to calculate cpu usage by processes for 1 hour
#Change the SECS to total seconds to monitor CPU usage.
#UNIT_TIME is the interval in seconds between each sampling
SECS=3600
UNIT_TIME=60
STEPS=$(( $SECS / $UNIT_TIME ))
echo Watching CPU usage... ;
# Collect data in temp file
for((i=0;i<STEPS;i++))
do
ps -eocomm,pcpu | egrep -v '(0.0)|(%CPU)' >> /tmp/cpu_usage.$$
sleep $UNIT_TIME
done
# Process collected data
echo
echo CPU eaters :
cat /tmp/cpu_usage.$$ | \
awk '
{ process[$1]+=$2; }
END{
for(i in process)
{
printf("%-20s %s\n",i, process[i]) ;
}
}' | sort -nrk 2 | head
#Remove the temporary log file
rm /tmp/cpu_usage.$$
The output resembles the following:
$ ./pcpu_usage.sh Watching CPU usage... CPU eaters : Xorg 20 firefox-bin 15 bash 3 evince 2 pulseaudio 1.0 pcpu.sh 0.3 wpa_supplicant 0 wnck-applet 0 watchdog/0 0 usb-storage 0