The-importance-of-being-idle asks: You have talked before about scheduling tasks at a specific time. Is there a way to launch tasks when the computer is idle, not just at a set time?
DistroWatch answers: It is possible to check how busy your CPU is and run certain programs when the processor is idle. However, this may not be the best solution if your goal is running programs without having them interfere with system performance. First, let's look at how to run a command when the CPU is relatively idle. There are a few ways to check to see how busy the CPU is, but one of the easiest is using the uptime command:
One command useful for keeping jobs out of the way is nice. The nice command tells the operating system to assign a lower priority to our job. The lower priority will help to keep the job from using the CPU while other, higher priority tasks, are active. Here is an example of the nice command in action, being used to run the apt-get program:
DistroWatch answers: It is possible to check how busy your CPU is and run certain programs when the processor is idle. However, this may not be the best solution if your goal is running programs without having them interfere with system performance. First, let's look at how to run a command when the CPU is relatively idle. There are a few ways to check to see how busy the CPU is, but one of the easiest is using the uptime command:
jesse@drew:~$ uptimeThe uptime program shows us the current time, how long the computer has been running, how many users are logged in and the processor's load average. The load average tells us how busy the CPU is. The uptime program displays three numbers for load averages, the first shows us how busy the CPU has been, on average, for the past minute, the second number indicates average processor load for the past five minutes and the third number indicates load for the past fifteen minutes. A load average of 1.00 would mean the CPU is constantly busy, a load of 0.00 would indicate the machine is practically idle. If we want, we can set a job to run only if the load average falls to a certain point, say 0.01. The following script checks the system load once a minute and, when the load drops below 0.05, it will run a job, provided on the command line, for us:
15:40:49 up 8:17, 3 users, load average: 0.92, 0.88, 0.85
#!/bin/bashNow there are problems with this approach. One being that we're constantly running checks, which is a little wasteful. Another is that if our load average never drops below 0.05 our task doesn't get to run. Another issue is that if we want to use the computer while our intensive task is running we either have to interrupt it or deal with reduced performance while our important job is running. In my opinion a better way to handle the situation is to run our special task whenever we want, but make sure it does not affect system performance.
if [ $# -lt 1 ]
then
echo usage $0 command
exit 0
fi
load=`cat /proc/loadavg | awk '{print $1*100}'`
while [ $load -gt 5 ]
do
sleep 60
load=`cat /proc/loadavg | awk '{print $1*100}'`
done
$@
One command useful for keeping jobs out of the way is nice. The nice command tells the operating system to assign a lower priority to our job. The lower priority will help to keep the job from using the CPU while other, higher priority tasks, are active. Here is an example of the nice command in action, being used to run the apt-get program:
nice apt-get updateShould a job already be running and we want to lower its priority, making it less intrusive, we can use the renice command. The renice command typically accepts two pieces of information, the new priority we want to assign and the process ID of the job we want to alter. Priority numbers are typically set in the range of -20 to 19, with 19 being the lowest possible priority. The following command assigns the lowest priority to process 3175:
renice -n 19 -p 3175While the nice and renice commands work on adjusting the priority of jobs in the CPU sometimes we find that the main bottle neck to performance is with the hard drive. For instance backup processes require a lot of disk access and we might want to keep them out of the way. For these sorts of tasks we have the command ionice. The ionice command works much the same way as nice and renice. With ionice we can either launch a new task with altered priority to the disk or we can make adjustments to processes already running. The ionice command recognizes three classes of priority: realtime (which is the most aggressive), best-effort (which is the default) and idle (which should not interfere at all with other tasks trying to access the disk). The ionice manual page tells us to make a process idle, we assign it a class of 3. In the following example we perform a backup using a polite, idle, disk priority:
ionice -c 3 tar czf mybackup.tar.gz ~/DocumentsIn this next example we tell the system to make sure the process with ID 4571 takes the lowest priority when accessing the disk:
ionice -c 3 -p 4571Should we wish to, we can use both the ionice and renice commands to make sure our job stays out of the way both when accessing the CPU and the disk. This next example begins a backup of our Documents directory and assigns the process both a low disk and low CPU priority:
ionice -c 3 tar czf mybackup.tar.gz ~/Documents &The above commands -- nice, renice and ionice -- affect the scheduling and priority of a task. There is another command, called CPUlimit, which throttles a process. (CPUlimit is usually not installed by default, but is available in many Linux repositories.) What's the difference between throttling and scheduling? Well, a task with a low priority can still use 99% of the CPU if nothing else is competing for system resources. The low-priority task only backs off when another process wants to step in. Throttling a process, on the other hand, forces that process to only use a limited per cent of the available CPU. We can limit a process to use only 10%, 20%, 50%, etc of the CPU, whether the machine is idle or busy. The CPUlimit command typically takes two pieces of information, a process ID of the job we want to throttle and the maximum percentage of the CPU we want to allow to the process. For example, the following limits the process 6112 to using just 25% of our CPU:
renice -n 19 -p $!
cpulimit -p 6112 -l 25The following commands start a new backup job and then throttles its CPU usage to 10%:
tar czf mybackup.tar.gz ~/Documents &
cpulimit -p $! -l 10 -b
Source : http://distrowatch.com/weekly.php?issue=20120521