Capture Process with highest CPU% in Variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capture Process with highest CPU% in Variable
# 1  
Old 01-23-2010
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:
Code:
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.

Code:
[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:

Code:
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
# 2  
Old 01-23-2010
Something like this?

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

# 3  
Old 01-23-2010
[solved]

@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?
Code:
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.

Code:
[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
# 4  
Old 01-23-2010
ps calculates the average time

top considers only the last second (by default) of a process
# 5  
Old 01-25-2010
@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.

Code:
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
# 6  
Old 01-26-2010
Quote:
Originally Posted by jaysunn
@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.

Code:
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
Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Capture PRSTAT based on CPU usage percentage

Hi, Recently i have write a simple script to capture CPU high usage based on prstat but i found out that it did capture correctly. I need to capture the rows that contains CPU usage more than 3%. Below line which i thought will capture CPU usage based CPU column in prstat(9th parameter) which is... (3 Replies)
Discussion started by: tharmendran
3 Replies

2. Emergency UNIX and Linux Support

CPU and memory utilization of a process, by process name

Can someone please help me with a script that will help in identifying the CPU & memory usage by a process name, rather than a process id.This is to primarily analyze the consumption of resources, for performance tweaking. G (4 Replies)
Discussion started by: ggayathri
4 Replies

3. Shell Programming and Scripting

Capture the running process for 2 hours

Hi, How can i capture the running process for 2 hours. Thanks in advance.:b: (1 Reply)
Discussion started by: sarathkumar
1 Replies

4. Shell Programming and Scripting

Store highest filename from ls command to a variable

I am sure it has been answered in some form or the other in this forum but trust me I have spent last couple of hours looking for an answer but can't find it. I want to do a very simple operation within my script. Let's say I have 4 files with the names file1.log, file2.log, file3.log and... (6 Replies)
Discussion started by: sssccc
6 Replies

5. Shell Programming and Scripting

Log Capture for Background Process

Hi , I am running a backgorund process called hello.sh ./hello & Now i need to capture the log file as it produces the output . i am not able to use " >> " nor " tee " to capture the output file / log file . Please let me know how can i do it ? Regards, Deepak Konnur (3 Replies)
Discussion started by: dskonnur
3 Replies

6. Programming

How to capture messages from child process?

Hi all, I'm new in programming, but want to start writing a simple GUI for linux console application,say, wget.(for educational purpose :) ). The question is: how to start child process from C++ code and then start capture messages from its stdout? Thanks in advance. (2 Replies)
Discussion started by: vahagn_iv
2 Replies

7. UNIX for Dummies Questions & Answers

how to get persistant cpu utilization values per process per cpu in linux (! top,ps)

hi, i want to know cpu utilizatiion per process per cpu..for single processor also if multicore in linux ..to use these values in shell script to kill processes exceeding cpu utilization.ps (pcpu) command does not give exact values..top does not give persistant values..psstat,vmstat..does njot... (3 Replies)
Discussion started by: pankajd
3 Replies

8. Solaris

start a process with the highest priority

hello, I have a process lauched by non-root user. how to lauch this process with a very high priority? I know this has to do with nice command but how to allow a user to lauch his process with a very high priority and without ROOT intervention? PS: this process is always lauched from a... (2 Replies)
Discussion started by: melanie_pfefer
2 Replies

9. Solaris

NAWK - setting a variable to the highest value

Hi, new to nawk so not sure how this works, I have a file with three values 23:36:18 - i need to set a variable called SCORE with the highest value from the file (i.e. 36) using nawk can anyone help? thanks (1 Reply)
Discussion started by: Pablo_beezo
1 Replies

10. Shell Programming and Scripting

capture the process id when starting a background process

Hello all, How do I start a background process and save the process id to a file on my system. For example %wait 5 & will execute and print the process id. I can't figure out how to get it to a file. I've tried: > filename 0>filename 1>filename. Any assistance is most appreciated. Thanks, Jim... (10 Replies)
Discussion started by: jleavitt
10 Replies
Login or Register to Ask a Question