Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I wrote a simple bash script that checks the load average values using 'uptime' every 5 minutes and writes a result to the text file. Everything is OK except one thing: every day from 12:00 to 13:00 I see text value 'average' instead of regular integer values.

How should I interpreter the 'average' as a result of 'uptime' command? And if I run 'uptime' from the command line everything is OK - I see regular integer values.

The source code below:

#!/bin/bash

sCurrentUptime="$(uptime | awk '{print $10}')"
iLength="${#sCurrentUptime}"
sUptime="${sCurrentUptime:0:iLength-1}"
iUptime=${sUptime/.*}
now="$(date)"
echo $now';'$iUptime

And here are the outputs (last collumns contains load average value):

Thu Apr 17 08:40:01 MSK 2014;0
Thu Apr 17 09:00:01 MSK 2014;2
Thu Apr 17 09:20:02 MSK 2014;3
Thu Apr 17 09:40:02 MSK 2014;3
Thu Apr 17 10:00:01 MSK 2014;2
Thu Apr 17 10:20:01 MSK 2014;3
Thu Apr 17 10:40:01 MSK 2014;1
Thu Apr 17 11:00:02 MSK 2014;2
Thu Apr 17 11:20:01 MSK 2014;3
Thu Apr 17 11:40:01 MSK 2014;2
Thu Apr 17 12:00:02 MSK 2014;3
Thu Apr 17 12:20:02 MSK 2014;average
Thu Apr 17 12:40:01 MSK 2014;average
Thu Apr 17 13:00:01 MSK 2014;average
Thu Apr 17 13:20:01 MSK 2014;3
Thu Apr 17 13:40:01 MSK 2014;1
Thu Apr 17 14:00:01 MSK 2014;2
Thu Apr 17 14:20:01 MSK 2014;3
Thu Apr 17 14:40:01 MSK 2014;2
Thu Apr 17 15:00:01 MSK 2014;2
Thu Apr 17 15:20:01 MSK 2014;3
Thu Apr 17 15:40:01 MSK 2014;1
share|improve this question
3  
can you copy-paste the outputs (both normal and with average), I always seem to get 'load average' in the output of uptime, therefore I am not sure what you are referring to. –  Anthon Apr 27 at 14:31
1  
Can you provide the exact commands you're using in your script? there's something you might be doing wrong when parsing the output of uptime. –  gniourf_gniourf Apr 27 at 14:56
    
sure, I have updated the post. –  Vlad Chaotic Apr 27 at 15:30

2 Answers 2

The output of uptime depends on the uptime itself, i.e.

On one system

$ uptime
17:35pm  up 5 days  9:24,  9 users,  load average: 0.30, 0.28, 0.28

thus, 12 fields.

On another system

uptime
17:36:15 up  8:44,  2 users,  load average: 0.09, 0.30, 0.41

And thus 10 fields. It may defer for your system, of course. I suppose that you switched on your machine at noon, which leads to an uptime of N days and 0 hours, where the latter is not display (or something like that).

As it seems that you want to have the last column anyhow, you can replace your awk command with

uptime | awk '{print $NF}'

NF is the number of fields, and $NF therefore the last field of the line. It think this is more error-proof.

If you want the first load average, instead of the third, then you can do

uptime | awk '{print $(NF-2)}'
share|improve this answer
    
thank you. I have just realized that 'average' originates from the 'uptime' result string and my awk command just sends it according to my 'print $10' instruction. I have never thought in that way )) Thanks a lot. –  Vlad Chaotic Apr 27 at 15:45
    
uptime | awk '{print $(NF-2)}' will give the comma as well. –  DK Bose May 1 at 11:00
1  
@DKBose True, you could solve that by an additional | tr -d ",". (Or in awk if you like) –  Bernhard May 1 at 11:27

For scripting purposes I find it easier to get the load averages directly from /proc/loadavg then to try and parse uptime's annoying output.

Example

$ cat /proc/loadavg
1.08 0.77 0.85 2/838 16771

From man proc:

   /proc/loadavg
        The first three fields in this file are load average figures 
        giving the number of jobs in the run queue (state R) or waiting for 
        disk I/O (state D) averaged  over  1, 5, and 15 minutes.  They are 
        the same as the load average numbers given by uptime(1) and other 
        programs.  The fourth field consists of two numbers separated by a 
        slash (/).  The first of these is the number of currently runnable 
        kernel scheduling entities  (processes,  threads).   

        The  value after  the slash is the number of kernel scheduling 
        entities that currently exist on the system.  The fifth field is 
        the PID of the process that was most recently created on the system.
share|improve this answer
    
Are you aware of any command that would return the info about the kernel scheduling entities? –  Bernhard Apr 27 at 18:46
    
@Bernhard - which scheduling entities are you referring to? –  slm Apr 27 at 18:48
    
Much of the data is spread around /proc. blog.tanelpoder.com/2013/02/21/… –  slm Apr 27 at 18:51
    
The 2/838 from your example. I understand /proc and how to use it. But this is just curiosity :) –  Bernhard Apr 27 at 19:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.