3

To an idea of CPU load average, I'm using uptime in a ksh script:

uptime | awk '{print $11}' | sed '$s/.$//' | read CPU

where I then use the variable CPU later.

The $11 part is to isolate the last five minutes part. But, I noticed today that this was not working. Specifically, the last five minutes part was returned with $9. The function is returning less parameters. This is because the machine was recently rebooted, and so uptime shows minutes since reboot instead of days and minutes.

Is there a way I can consistently get only the last five minutes part of uptime?

6 Answers6

11
cut -d ' ' -f2 /proc/loadavg

/proc/loadvg is the source of data for uptime, w, who and others. It has a simpler format and the numbers always have a dot before the decimal part (uptime and such use the current locale, so you may find something like

load average: 0,18, 0,26, 0,30

which are harder to parse

plus is faster by an incredibly low factor! ;-)

Riccardo Galli
  • 12,419
  • 6
  • 64
  • 62
  • Are there any downsides to this approach? On the surface it does seem a much better solution that using upstart, even if it doesn't technically answer the question. Is `/proc/loadavg` system-specific or require certain permissions? – skeggse Jan 18 '14 at 20:46
  • Doesn't work on *BSD, but should work on every linux distribution, since procps use it (uptime is part of procps for example) https://gitorious.org/procps/procps/source/d06aaaaf2bd8f3b5f0235e75f4f04c0ad69c7d6d:proc/sysinfo.c – Riccardo Galli Jan 18 '14 at 21:58
3

Try to split away the text before "Load Average", and then use awk on the remaining part.

uptime | sed 's/.*load average: //' | awk -F\, '{print $2}'
Benedikt Köppel
  • 4,853
  • 4
  • 32
  • 42
1

It might be simpler to read the 2nd to last field rather than the 9th or the 11th:

uptime | awk '{print $(NF-1)}' FS=,
William Pursell
  • 204,365
  • 48
  • 270
  • 300
0

This little shell function should work with bash or ksh(93)

    function loadavg {
        typeset minutes=$1 t1=$(uptime)
        echo ${t1#*load average: } | (
            IFS=', ' && read L1 L5 L15
            case $minutes in
            (1)  echo $L1;;
            (5)  echo $L5;;
            (15) echo $L15;;
            ("") echo $L1 $L5 $L15;;
            (*)  echo "usage: loadavg [ 1 | 5 | 15 ]" 1>& 2
            esac                
        )
    }

Explanation:

This code uses IFS to split the string after "load average: " into three fields. 'typeset' and the subshell isolate the function variables from other shell variables.

The following simplifies the result, and just returns the answer to the original question:

function load5 {
    typeset t1=$(uptime)
    echo ${t1#*load average: } | (
        IFS=', ' && read L1 L5 L15
        echo $L5            
    )
}
Henk Langeveld
  • 8,088
  • 1
  • 43
  • 57
0

This could give you best result I am using it to get my load average for every 5 minutes:

$ uptime | awk '{ print $11 }'| tr -d ','

0

So, I was having trouble writing one that worked in both Linux and Mac OS X. After much fighting I came up with this:

uptime | sed 's/.*load average[s]*://' | awk '{print $3}'

Hope this is helpful for someone.