0

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, in case the CPU usage is above 90% I need to list the top 5 Processes that are using the CPU.

How do I achieve this?

2

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).

  • 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
  • That's not exactly the contrast point -- ps gives you a weird process lifetime vs. CPU seconds which is not necessarily indicative of what's causing high CPU "recently". topas, like top, gives you the % over a much short reasonable that is intuitively usually what people want. – covener Apr 3 '17 at 20:39
0

For a deep dive, the best course of action here is to run

tprof -skex sleep 10

which will give you a report detailing the processes using high CPU, then drill down into usage by function and shared libraries.

-1

you can use below command it will match with topas output

ps -ef | egrep -v "STIME|$LOGNAME" | sort +3 -r | head -n 15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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