On Wed, 5 Aug 2009, Nick Olsen wrote: > Guess I'm just not really getting the whole lock file thing, If its not > there SHOULD this script make it? Also, If I make a file called > update-centos.lock it complains that the lock file already exists when I > run the script but still runs the rsync command. I'm really not well > versed when it comes to linux lock files and scripting. And would really > need someone to hold my hand the whole way :s This is what I make from > David's comments, I'm sure I've done it wrong. > > #!/bin/sh > > ### update-centos.sh > #!/bin/bash > > ### Lock file > if [ -f /tmp/update-centos.lock ] > then echo "Lock file exists" && exit 1 else > date >> /tmp/update-centos.lock > fi > > ### Big long rsync command ### > echo "Starting centos rsync" > screen -A -m -d -S centos rsync -vaH --numeric-ids --delete --delete-after --delay-updates --progress rsync://linux.mirrors.es.net/centos/ /home/www/centos > > ### Remove lock file > /bin/rm -rf /tmp/update-centos.lock > ### end update-centos.sh You're on the right track, Nick. The point of the lock file is to say "Hey, there's already a copy of this script running." Two gotchas: * My script uses /bin/bash, yours is using /bin/sh. That means you're restricted to sh syntax, and so you can't use any bash extentions. * My script has a line break between > then echo "Lock file exists" && exit 1 and > else The idea is this: 1) The script starts 2) Check to see if the lock file already exists a) The lock file exists. This means another instance is running, so quit and leave it alone. b) The lock file doesn't exist. 3) Create the lock file. This way, if cron starts another run before this one finishes, it will get to step 2a and exit. 4) Do the rsync 5) We're done. Remove the lock file so that the next time cron starts the update, it will work. I'll rewrite the script with more comments: #!/bin/bash # Does the lock file exist? if [ -f /tmp/update-centos.lock ] # It does exist. # Exit here so we don't screw up a run in-progress then echo "Lock file exists" && exit 1 else # No, it doesn't exist. # Create the lock file so another run won't start. date >> /tmp/update-centos.lock fi ### Now we can do the actual rsync. At this point, we know that only one ### copy of the script is running. ### Big long rsync command ### echo "Starting centos rsync" screen -A -m -d -S centos rsync -vaH --numeric-ids --delete --delete-after --delay-upd$ # Now we're done. Remove the lock file. /bin/rm -rf /tmp/update-centos.lock Did I manage to make that clear as mud? DR -- David Richardson <david.richardson at utah.edu> "There are two kinds of statistics: the kind you look up and the kind you make up." -- Archie Goodwin, Death of a Doxy