1826605 Members
3283 Online
109695 Solutions
New Discussion

Re: %CPU Info in TOP

 
SOLVED
Go to solution
Scott Clement_2
Advisor

%CPU Info in TOP

Is there a way to get to the %CPU information from top through a shell script short of running top once and read to the line. Looking for a system callable routine from a shell script.
10 REPLIES 10
Sundar_7
Honored Contributor

Re: %CPU Info in TOP

Are you looking for the global CPU usage or per process CPU usage ?

Per process CPU usage can be extracted using ps command

UNIX95= ps -ef -o pid,pcpu,args | sort -nbk 2

The above command line will list the processes sorted based on their CPU usage.

If you are looking for global CPU usage, you could use sar , vmstat or top itself

Learn What to do ,How to do and more importantly When to do ?
Scott Clement_2
Advisor

Re: %CPU Info in TOP

I am looking for the top CPU process without having to do to much file editing.
TOP sorts based on %CPU. And yes I can get it from a top -d 1 command and read the file until I get to the line I can and grab it then. I am looking for something easier?
Sundar_7
Honored Contributor

Re: %CPU Info in TOP


UNIX95= ps -ef -o pid,pcpu,comm | sort -nbk 2 | tail -1 | read PID PCPU PROCESS

echo "Process $PROCESS with pid: $PID is the top CPU process using $PCPU% of CPU time"

Learn What to do ,How to do and more importantly When to do ?
Scott Clement_2
Advisor

Re: %CPU Info in TOP

Thats great if I have the -o option. I am running on a HP-UX 11i system and it does not have the -o format option available to select the columns that I want to see.
Massimo Bianchi
Honored Contributor
Solution

Re: %CPU Info in TOP

Hi,
that trick works.

YOu must pay care in the UNIX95= , and note the space after the equal.

This enables the xpg4 compatibility :)

HTH,
Massimo
Muthukumar_5
Honored Contributor

Re: %CPU Info in TOP

If you want to get process which is consuming more CPU% then use top command itself as,

top -d 1 -n 1

-n --> number of process per

or just as,

top -d 1 -n 1 -f /var/tmp/cpuhigh.pid

If you put in cron it will update the file upto the stage.

HTH.

Regards
Muthu.

Easy to suggest when don't know about the problem!
Scott Clement_2
Advisor

Re: %CPU Info in TOP

The ps command works well. I just figure out how to turn on the -o option. Using top would be to much work to get the line of data that I want. We are looking to check if 1 single process is using the CPU to much. Like a process stuck in a loop.
Muthukumar_5
Honored Contributor

Re: %CPU Info in TOP

you can use -q option on top to reduce the time to take to operate. But it is capable to privillaged users. And more you can use -h to hide the cpu informatios as combined one.

And top is little bit lagging on performance comparision on your requirement as,
test # time top -q -h -d 1 -n 1

CPU TTY PID USERNAME PRI NI SIZE RES STATE TIME %WCPU %CPU COMMAND
0 ? 725 root 152 20 285M 37220K run 37:29 0.39 0.39 ns-httpd

real 0m1.12s
user 0m0.00s
sys 0m0.08s
test # time UNIX95= ps -ef -o pid,pcpu,comm | sort -nbk 2 | tail -1
725 0.37 ns-httpd

real 0m0.23s
user 0m0.01s
sys 0m0.02s

Regards
Muthu
Easy to suggest when don't know about the problem!
Eric Herberholz
Advisor

Re: %CPU Info in TOP

To script looking for cpu hogs:
# top -d 1 -s 1 -n 5 -f /tmp/top.$$;tail -n 5 /tmp/top.$$ | awk '{if ($(NF-1) > 40) print $(NF-1)," ", $NF}'; rm /tmp/top.$$

(^ this will look for the top 5 processes and then check to see if any of them are using more than 40 percent of a cpu.

Or to sort [the top 50] processes by cpu and then by cpu percentage, use:
# top -d 1 -s 1 -n 50 -f /tmp/top.$$;tail -n 50 /tmp/top.$$ | sort -rn +0 +11; rm /tmp/top.$$

Rory R Hammond
Trusted Contributor

Re: %CPU Info in TOP

Two Scripts

XXXXXXXXXXXXXX top XXXXXXXXXXXXXXXXX
#!/bin/sh
top -n 1 -h -f /tmp/top$$
awk ' BEGIN {FOUND=0;COUNT=1}
(FOUND <= 0) {
FOUND=index($0, "LOAD")
HEADING=sprintf ("%s \n", $0 )
next
}
{
printf ("%s", HEADING )
printf ("%s \n", $0 )
exit
}
' /tmp/top$$
rm /tmp/top$$

xxxxxxxxxxxxxxxxxx vmstat XXXXXXXXXXXXXX
#!/bin/sh
#gather memory stats
vmstat 2 4 |tail -3 |awk '
{
for (i =1; i <=3; ++i)
{
split($18,c_id," "); c_id[i]
c_idtot += c_id[i]
c_idavg = c_idtot / 3
}
}
END{printf("CPU IDLE=%2d \n", c_idavg)}'




There are a 100 ways to do things and 97 of them are right