Althaf
Althaf

Reputation: 21

KSH Script to get the CPU usage

Can somebody please help me to write a KSH Script to get the CPU usage of the AIX server ?

Here I want my script to get the Current usage of CPU that time it is executed

Upvotes: 0

Views: 4505

Answers (2)

pitseeker
pitseeker

Reputation: 2543

There are a number of tools on AIX (and elsewhere) to get the current CPU usage.

  • nmon
    On AIX (and Linux) you have nmon. This gives very detailed infos on memory, cpu usage, disk usage, etc. It is normally used as an interactive tool.
  • sar
    call sar -u 1 1 to get the current cpu usage. See the manual page of sar for a whole lot of options. Depending on your installation you need to be root or add your user to the group "adm".
  • Just call w -u. It outputs a little bit more than you ask for. If you don't need that you can use awk/sed/cut to cut it away.

Upvotes: 1

pfnuesel
pfnuesel

Reputation: 15300

I use the following script in , but I just tried it in and it works all the same:

top -bn2 | grep 'Cpu(s)' | sed -n '2s/.*, *\([0-9.]*\)%* id.*/\1/p' | awk '{print "CPU: " 100 - $1" %"}

You can also use

top -bn1 | grep 'Cpu(s)' | sed -n 's/.*, *\([0-9.]*\)%* id.*/\1/p' | awk '{print "CPU: " 100 - $1" %"}'

for faster response, but the result will be less accurate.

Upvotes: 0

Related Questions