Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

If I run top -p <myPID> -n 1 with a terminal that isn't wide enough, the CPU utilization % is omitted from the output. When trying to find a process' cpu utilization via bash scripting, this is a huge problem, as the script won't work if the terminal you're running it in isn't wide enough.

ps -oe pcpu,pid,cmd will give me the total average cpu, but not a running utilization.

Is there a command that I can use to get the current cpu utilization of a process that isn't dependent on terminal width?

share|improve this question
add comment

4 Answers 4

up vote 4 down vote accepted

The sysstat package includes mpstat. Running

mpstat 2 | awk '{print $11}'

Gave me the idle-time percentage which seems like the inverse of what you want, so you might need to do a little work on mpstat output:

8 % mpstat 2 | awk '{print $11}'

%idle
100.00
99.50
100.00
100.00
100.00
100.00

I had mpstat on my Slackware 11 system, but it didn't appear on my Arch system until I did pacman -S sysstat

share|improve this answer
add comment

You could just use the f key to adjust the columns displayed. Remove some columns and add the CPU%. You should be able to see it then.

share|improve this answer
    
He's trying to do this from a script. As far as I can tell, top doesn't have a command-line option to specify columns. –  cjm Jun 11 '11 at 3:58
    
Well how about a Python script? Use the CPUMeasurer object from the procfs module. –  Keith Jun 11 '11 at 5:54
    
Yea, you can't use the interactive features of top from a script, so this won't work. –  Cory Klein Jun 13 '11 at 15:11
    
My script doesn't use top, it computes CPU utilization directly the same way top does. –  Keith Jun 13 '11 at 20:16
add comment
ps u -p <PID>

Works fine. you also might try using awk with it:

 pid=16707; ps u -p $pid --no-heading | awk -v pid=${pid} -F" " '{print "CPU usage for "$11": "$3}'
share|improve this answer
    
This doesn't work either. I need the current cpu utilization. ps just gives the total average historical utilization. –  Cory Klein Jun 13 '11 at 15:12
    
Try this one: top -n 1 -p 13145 | grep PID --after 1 | grep -v PID | awk -F" " '{print $1}' –  laebshade Jul 5 '11 at 23:36
    
I can't use top, because if the window running the shell is too narrow, top won't display the CPU info. –  Cory Klein Jul 6 '11 at 22:11
add comment

In your script, set the COLUMNS environment variable to be sufficiently high to get the output you need.

export COLUMNS=100
top -p ... 

You could also change the COLUMNS var just for the top invocation thusly:

COLUMNS=100 top -p ...
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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