LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 05-01-2012, 05:14 AM   #1
wojcieche
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.
Code:
ps -C processname -o %cpu=

The only solution I came up with is this:
Code:
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:
Code:
#!/bin/bash
cpuLoad=$(top -n 1 | grep processname | awk '{print $10}')

if [ $(echo $cpuLoad) -lt 40 ]
	then echo CPU USAGE IS LOW
fi
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:
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 05:23 AM.
 
Old 05-01-2012, 05:19 AM   #2
wojcieche
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:
Code:
		cpuLoad=$(top -n 1 | grep processname | awk '{print $10}')

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

		if [ $(echo $cpuLoad) -lt 40 ]
			then CPU USAGE IS LOW
		fi
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 05:22 AM.
 
  


Reply


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 07:48 AM
process cpu usage ahmedb72 Linux - General 9 09-07-2010 02:22 PM
How to get %CPU usage of a process devershetty Linux - Newbie 13 08-16-2008 11:51 AM
process cpu usage hamedrafi Programming 5 06-11-2007 06:32 PM
how to find max memory usage for process in Linux by command line kashameni Linux - Newbie 9 06-01-2007 01:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:09 PM.

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