Capture Process with highest CPU% in Variable

I am trying to capture the process on a red hat linux system with the highest cpu utilization into a variable. I have been playing with ps command but really do not think I am making the correct progress.

For instance:

top - 09:01:16 up 63 days, 18:29,  2 users,  load average: 0.39, 0.49, 0.48
Tasks: 175 total,   1 running, 174 sleeping,   0 stopped,   0 zombie
Cpu(s): 13.7% us, 15.1% sy,  0.0% ni, 70.0% id,  0.8% wa,  0.3% hi,  0.0% si
Mem:  15337328k total,  6674076k used,  8663252k free,   388060k buffers
Swap:  6144852k total,        0k used,  6144852k free,  2713480k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                                               
 2134 nobody    15   0 69272  52m  11m S   14  0.4   2:07.28 httpd 

Above the red 14% is what I am after. I have been using this command to try to get it, however not sure it is correct.

[root@vmradio01 ~]# ps -eo pid,user,%cpu,args --sort %cpu |grep httpd | tail -n1 | awk '{print $3}'| awk -F \. '{print $1}'
1

My goal is to capture the process consuming the most cpu usage. So I am running this:

cpuusage=`[root@vmradio01 ~]# ps -eo pid,user,%cpu,args --sort %cpu |grep httpd | tail -n1 | awk '{print $3}'| awk -F \. '{print $1}'`

Any thoughts or suggestions please?

Jaysunn

Something like this?

ps -eo pid,user,%cpu,args --sort %cpu | awk '/httpd/{n=n<$3?$3:n}END{print n}' 

@Franklin52,

Thank you kindly for your expertise. This is exactly what I was looking for. I really appreciate your time.

Jaysunn

---------- Post updated at 10:12 AM ---------- Previous update was at 09:55 AM ----------

Franklin,
Just a question. When I use top, I don't see a decimal point. Why is this command showing one?

top - 10:10:22 up 11 days, 17:28,  1 user,  load average: 0.92, 0.80, 0.71
Tasks: 121 total,   1 running, 120 sleeping,   0 stopped,   0 zombie
Cpu(s): 10.6% us,  8.8% sy,  0.0% ni, 75.4% id,  5.0% wa,  0.2% hi,  0.0% si
Mem:  15337328k total,  6366284k used,  8971044k free,   417988k buffers
Swap:  6144852k total,        0k used,  6144852k free,  4320512k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                                               
24166 nobody    15   0 67676  50m  11m S    32  0.3   0:54.25 httpd   

I never seem to get a high number like I see in the top output.

[root@vmradio05 ~]# ps -eo pid,user,%cpu,args --sort %cpu | awk '/httpd/{n=n<$3?$3:n}END{print n}'
1.5

Any Ideas?

Jaysunn

ps calculates the average time

top considers only the last second (by default) of a process

@Franklin,

I have really tried to research your code via google and this forum. I am really having a tough time deciphering the portion in red.

ps -eo pid,user,%cpu,args --sort %cpu | awk '/httpd/{n=n<$3?$3:n}END{print n}'

Could you kindly elaborate? I understand that we are searching for httpd. Then you are assigning n to some less than 3. However I may be completely wrong. Your assistance is a greatly appreciated.

Jaysunn

n=n<$3?$3:n

This is a short form of an if expression and is called a conditional expression.
You can have a read of this regarding this stuff:

Conditional Exp - The GNU Awk User's Guide

Regards