Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Help with bash script - Need to get CPU usage as a percentage

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-24-2009
graysky graysky is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 15 June 2015, 7:52 AM EDT
Posts: 26
Thanks: 1
Thanked 0 Times in 0 Posts
Help with bash script - Need to get CPU usage as a percentage

I'm writing a bash script to log some selections from a sensors output (core temp, mb temp, etc.) and I would also like to have the current cpu usage as a percentage. I have no idea how to go about getting it in a form that a bash script can use. For example, I would simply look in the output of htop, but again, I need to assign the % load to a variable in a bash script.

Does anyone know how I can do it?
Sponsored Links
    #2  
Old Unix and Linux 11-24-2009
mkastin mkastin is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 3 November 2012, 1:28 AM EDT
Posts: 52
Thanks: 1
Thanked 0 Times in 0 Posts
This will assign user cpu% used to variable c

This assumes, you only have 1 cpu and that you are only concerned with user cpu %


Code:
c=`iostat | awk '{if(NR==4) {print $1}}'`

Sponsored Links
    #3  
Old Unix and Linux 11-25-2009
graysky graysky is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 15 June 2015, 7:52 AM EDT
Posts: 26
Thanks: 1
Thanked 0 Times in 0 Posts
My distro doesn't have iostat Linux I need to figure out which package contains it.
    #4  
Old Unix and Linux 11-25-2009
mkastin mkastin is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 3 November 2012, 1:28 AM EDT
Posts: 52
Thanks: 1
Thanked 0 Times in 0 Posts
sysstat
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Kill -9 within Bash script kicks out usage info mglenney Shell Programming and Scripting 1 07-09-2009 03:35 PM
awk script to count percentage from log file justbow Shell Programming and Scripting 8 05-08-2009 01:17 PM
Need an AWK script to calculate the percentage bobprabhu Shell Programming and Scripting 8 11-07-2008 04:42 AM
how to make percentage of a running script? jimmbp Shell Programming and Scripting 1 01-31-2006 01:41 PM
How to determine cpu&memory percentage usage per user hp-ux-user HP-UX 4 03-10-2005 06:53 AM



All times are GMT -4. The time now is 09:51 PM.