Register
It is currently Wed Sep 17, 2014 1:32 am

Output CPU load


All times are UTC - 6 hours


Post new topic Reply to topic  [ 9 posts ] 
Author Message
 PostPosted: Sun Nov 07, 2010 3:48 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
I'm trying to output cpu load as a percentage which I will probably end up using in a file, from cli I can get it using

Code:
ps aux|awk 'NR > 0 { s +=$3 }; END {print s"%"}'


however in a bash script i've got a ` character wrong somewhere

Code:
#!/bin/bash

LOAD=`ps aux|awk 'NR > 0 { s +=$3 }; END {print s"%"}'

$LOAD > /tmp/cpuload

sleep 1


./load: line 3: unexpected EOF while looking for matching ``'
./load: line 8: syntax error: unexpected end of file

an ideas?


Top
 Profile  
 PostPosted: Sun Nov 07, 2010 4:58 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Add a closing back-tick (`) at the end or the line where you define your LOAD variable
(the error says it is missing a back-tick at line 3...)
apart from that, I don't know what you're trying to accomplish with the next line ($LOAD > file.txt)
This will try to execute something with the name of your current CPU usage (don't confuse with load, which is something different)
Running for example: 9.5% > file.txt
Which wouldn't be a valid command.


Top
 Profile  
 PostPosted: Sun Nov 07, 2010 5:16 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
Ahh what i'm looking for is CPU usage like you get in 'TOP' which I will now output to a LCD so want to do something like

Code:
#!/bin/bash
LOAD=`ps aux|awk NR > 0 { s +=$3 }; END {print s"%"}``

lcdout "$LOAD"


I've added an extra backtick but it didn't make any diference?


Top
 Profile  
 PostPosted: Sun Nov 07, 2010 5:27 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 546
Hi,

why are you using old-style backquotes instead of usual $( ) ?


Top
 Profile  
 PostPosted: Sun Nov 07, 2010 6:00 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
I recommend using sa/sar for this as it can be easily configured for cpu % use. This is best when it's a remote server or collecting data from several servers.

If it's not for a server there are several desktop aps for displaying cpu % use

Just fixing the errors in your script though;
Code:
#!/bin/bash

LOAD=$(ps aux|awk 'NR > 0 { s +=$3 }; END {print s"%"}')

echo $LOAD > /tmp/cpuload

sleep 1


or

Code:
#!/bin/bash
ps aux|awk 'NR > 0 { s +=$3 }; END {print s"%"}' > /tmp/cpuload
sleep 1


I hope that helps
DW


Top
 Profile  
 PostPosted: Sun Nov 07, 2010 6:12 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
old code:
Code:
LOAD=`ps aux|awk 'NR > 0 { s +=$3 }; END {print s"%"}'

new code:
Code:
LOAD=`ps aux|awk NR > 0 { s +=$3 }; END {print s"%"}``

BrianUK wrote:
I've added an extra backtick but it didn't make any diference?

No you didn't. You've removed the single quotes (') from awk and you've added 2 backticks (`) at the end of the line.
So now you're still stuck with 3 back ticks on 1 line...
I suggest the same as my previous poster. use $() instead of backticks to evaluate a command in bash.
Code:
var=$(command args)


Top
 Profile  
 PostPosted: Sun Nov 07, 2010 6:37 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
Works a treat many thanks, i'm trying to get it to update every second but not working for some reason

Code:
#!/bin/bash

LOAD=$(ps aux|awk 'NR > 0 { s +=$3 }; END {print s"%"}')

(while true; do

lcdload "$LOAD"

sleep 1

done


Top
 Profile  
 PostPosted: Sun Nov 07, 2010 8:07 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
You fill your variable LOAD just once, and display the same result every second. I don't think that is what you want, right? :)


Top
 Profile  
 PostPosted: Sun Nov 07, 2010 11:00 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
ha ha no i want it to refresh every second


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP