Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-01-2012, 06:14 AM   #1
LQ Newbie
Registered: May 2012
Posts: 3

Rep: Reputation: Disabled
Question How to reliably get current process CPU usage from command line

OK. So I have been googling a lot, also this forums, but I still do not have answer for a presumably easy question.

How to reliably get current CPU usage by a process.

I do not mean ps command below - because it returns an average CPU usage in whole process lifetime. I need a snapshot of current usage.
ps -C processname -o %cpu=

The only solution I came up with is this:
top -n 1 | grep processname | awk '{print $10}'
But it is not reliable, because top command returns a number of rows limited to the screen hight and sorts by cpu usage decreasingly - so if a process uses 0% of CPU, it is on the end of the top listing and does not show on the list. Therefore in such situation this command returns empty string.

The code above would work if I could sort top's command listing by pid or start time. Is it possible? If it is not possible, how to reliably get current CPU usage of a process?

P.S. Showing the bigger picture, the point is to make this bash script work:
cpuLoad=$(top -n 1 | grep processname | awk '{print $10}')

if [ $(echo $cpuLoad) -lt 40 ]
	then echo CPU USAGE IS LOW
This script does not work well, because if cpuLoad variable returns empty string then an error shows up:
./test: line 3: [: -lt: unary operator expected
I can detect the situation where empty string instead of CPU load is returned with such code:
if [ ! -n "$cpuLoad" ]
but I do not have a clue how to write a code with combined conditions that will work like this:

IF cpuLoad is empty OR cpuLoad < 40 THEN...

The problem with such statement is that upon attempt to evaluate second condition an error will show up because the cpuLoad variable is empty, while value is expected.

Last edited by wojcieche; 05-01-2012 at 06:23 AM.
Old 05-01-2012, 06:19 AM   #2
LQ Newbie
Registered: May 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Solution to the code from the second part of the post

OK, I found a solution for the code in the second part. It should go like this:
		cpuLoad=$(top -n 1 | grep processname | awk '{print $10}')

		if [ ! -n "$cpuLoad" ]
			then cpuLoad=0

		if [ $(echo $cpuLoad) -lt 40 ]
If the empty string is returned, it means that CPU usage of the process is near or equal to zero, so I just set it to zero, so the second evaluation of the condition can work smoothly.

I found the solution before posting the OP, but decided to publish it as it might be useful to someone.

But the questions from first part of my OP remain: How to reliably get current CPU usage of a process?
Can somebody help?

Last edited by wojcieche; 05-01-2012 at 06:22 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
CPU Usage of a process matt9949 Linux - General 2 07-15-2011 08:48 AM
process cpu usage ahmedb72 Linux - General 9 09-07-2010 03:22 PM
How to get %CPU usage of a process devershetty Linux - Newbie 13 08-16-2008 12:51 PM
process cpu usage hamedrafi Programming 5 06-11-2007 07:32 PM
how to find max memory usage for process in Linux by command line kashameni Linux - Newbie 9 06-01-2007 02:39 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:31 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration