Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

For reasons I won't get into, I need to copy directories so long as the average system load is low. Can someone help me write a BASH script that will copy the contents of a directory, but check to make sure the average system load is below X before copying each file, and if not, wait Y seconds and try again?

share|improve this question
    
@akira: "So long as" is just as valid as "as long as" (also, why did you change it in the body of the question but not the title?). –  Dennis Williamson May 26 '10 at 5:53
    
@dennis: didnt see it there. i ll roll it back. –  akira May 26 '10 at 6:36

3 Answers 3

up vote 2 down vote accepted

Can you just run rsync or whatever copy command you want to use combined with nice to set processor priority and/or ionice to set io priority.

share|improve this answer

just for the fun of it (and yes, i know that it does not try to copy the file again in case it went to sleep mode, i will leave that for your own amusement; walking over "$@" comes to mind + shifting):

#!/bin/bash

for i in file1 file2 file3 etc
do

    LOAD=$(uptime | awk '{ sub(/,.*/, "", $9); print $9 * 100.0  }' )

    if [ $LOAD -lt 85 ]
    then
        echo "copy $i to wherever"
    else
        echo "sleep since load is $LOAD"
        sleep 5
    fi 
done

by using the load average of the last minute and only copying if the system is 85% idle, it will do something.

anyway, i would just use rsync with throtteling (as dennis mentioned) plus set a high nice value (equals to low priority) to the rsync process (as zoredache mentioned) and let the os do the scheduling (which is designed to do just that).

share|improve this answer

There is an option of rsync that limits I/O bandwidth:

--bwlimit=KBPS          limit I/O bandwidth; KBytes per second
share|improve this answer

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.