How to determine system is idle

Eris Caffee eris-redhat-list at eldalin.com
Sat Aug 27 13:33:00 UTC 2005


> I have a query - Is there any wayout from which we would determine that
> the linux machine had been idle for some time now, and in the meanwhile,
> we could start some background process to do some housekeeping jobs - or
> something of that sort.....

Yes, this is very easy in Linux.  There is a file on every Linux system
named /proc/loadavg which contains the 1 minute, 5 minute, and 10 minute
load averages for the system.  The load is the number of processes that
are waiting to run on the CPU, but which are blocked by a currently
running process, and the X minute load average is the average number of
processes that were waiting at any given instant over the past X minutes.
So, for example, a 5 minute load average of 1.0 says that over the past 5
minutes there was normally 1 process ready to run that was being blocked.

To read the /proc/loadavg file from the command line, you can use the cat
command, like this:

[eris at mozart eris]$ cat /proc/loadavg
0.43 0.28 0.14 2/124 30551

This shows a 1 minute load average of 0.43, a 5 minute average of 0.28 and
a 10 minute average of 0.14.  It is generally best if the load is less
than 1, so this shows that while my system is doing a few things, it is
not overburdened.  You will have to decide for yourself what it means for
your system to be "idle", but I would say that if the load average is less
than 0.10 then it is fairly idle.

You can find documentation about the /proc/loadavg file, and the other
files in the /proc directories, in Chapter 5 of the Red Hat Enterprise
Linux Reference Guide, which you can find here:
http://www.redhat.com/docs/manuals/enterprise/

If you had a shell script and you wanted to read the load averages to
determine whether or not to start the background job, you might use code
like this:

LoadAvg1=$(awk '{print $1 * 100}' /proc/loadavg)
LoadAvg5=$(awk '{print $2 * 100}' /proc/loadavg)
LoadAvg10=$(awk '{print $3 * 100}' /proc/loadavg)

Threshhold=10                # This is .10 (the desired load average)
times 100
if [ $LoadAvg5 -le $Threshhold ] ; then
    do_housekeeping
fi

This will start the do_housekeeping program if the system has been idle
for 5 minutes.  Notice that I multiplied the real load averages and the
Threshhold by 100 to convert everything to integers since shell variables
cannot be decimal numbers.

Eris Caffee





More information about the redhat-list mailing list