Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In order to monitor the CPU usage in an AIX server I'm using the following script that is executed every 10 mins.

lparstat 2 10 > cpu
usage=$(tail -10 cpu | awk 'BEGIN {sum=0;} {sum+=$4} END{print int(100-sum/10)}')


if [[ $usage -ge 90 ]]; then
# mail the error and cpu file to admin
# displaying this for testing purposes
echo "CPU usage off the charts!!!"
cat cpu
fi

However, incase the CPU usage is above 90% I need to list the top 5 Processes that are using the CPU.

How do I achieve this?

share|improve this question
add comment

2 Answers

You can list the top 5 in a fairly readable way by limiting the columns, sorting them with the highest CPU usage first, and then truncating to the first 5 (using head -6, since we also want to include the headers):

ps -eo pcpu,pid,args | sort -k 1 -r | head -6

The output looks something like this:

%CPU   PID COMMAND
 2.0 30531 -bash
 0.0 30673 head -6
 0.0 30672 sort -k 1 -r
 0.0 30671 ps -eo pcpu,pid,args
 0.0 30670 [flush-253:0]

You might also want to look into GNU top's batch mode (-b).

share|improve this answer
    
Unfortunately in the AIX system that I'm uing top is unavailable and has topas. ps -eo pcpu,pid,args | sort -rk1 | head -6 does give the output, but the result doesn't match with the result of topas. –  debal Oct 10 '13 at 8:15
    
@debal Why must the result match topas? –  Chris Down Oct 10 '13 at 8:16
    
both these commands tend to do the same thing, wouldn't it be surprising if the two results don't match? topas is giving a realtime view of the CPU usage, ps -eo pcpu,pid,args | sort -rk1 | head -6 is giving for a particular point of time, so at that instance it only makes sense if both these commands provide the same result. Which is not the case. –  debal Oct 10 '13 at 8:20
add comment

you can use below command it will match with topas output

ps -ef | egrep -v "STIME|$LOGNAME" | sort +3 -r | head -n 15
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.