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?

  • @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

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.


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

--bwlimit=KBPS          limit I/O bandwidth; KBytes per second

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):


for i in file1 file2 file3 etc

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

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

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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