I run a private centos mirror. Using rsync from the es.net mirror
I'm sure you get this all the time, but how do I use lock files with the rsync client to keep multiple from spawning because of cron jobs?
My mirror also hosts ubuntu and a few others which run at the same time, if this info is needed.
Right now the cron job just SH's a script containing a screen call with the rsync command in it. And I would like to keep it this way. At least that sh script. What im really looking for is a script that checks for the lockfile if its running. And then if its not SH's the script. I would have the cron job run that script, and that script would run my sh script called centos.sh located in /root/
Server is run on centos 5.3 i386
Let me know any other info needed
On Wed, 5 Aug 2009, Nick Olsen wrote:
I run a private centos mirror. Using rsync from the es.net mirror
I?m sure you get this all the time, but how do I use lock files with the rsync client to keep multiple from spawning because of cron jobs?
My mirror also hosts ubuntu and a few others which run at the same time, if this info is needed.
Right now the cron job just SH?s a script containing a screen call with the rsync command in it. And I would like to keep it this way. At least that sh script. What im really looking for is a script that checks for the lockfile if its running. And then if its not SH?s the script. I would have the cron job run that script, and that script would run my sh script called centos.sh located in /root/
On my mirror, I have a separate script for each project I mirror. My update-centos.sh script looks like this:
### 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 ###
### Remove lock file /bin/rm -rf /tmp/update-centos.lock ### end update-centos.sh
I also have /etc/rc.local remove the lock files for all my scripts (in case I reboot during a run): # /etc/rc.local /bin/rm /tmp/update.lock >/dev/null 2>&1
Hope this helps. DR
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
-----Original Message----- From: centos-mirror-bounces@centos.org [mailto:centos-mirror-bounces@centos.org] On Behalf Of David Richardson Sent: Wednesday, August 05, 2009 5:59 PM To: Mailing list for CentOS mirrors. Subject: Re: [CentOS-mirror] lock files
On Wed, 5 Aug 2009, Nick Olsen wrote:
I run a private centos mirror. Using rsync from the es.net mirror
I?m sure you get this all the time, but how do I use lock files with the rsync client to keep multiple from spawning because of cron jobs?
My mirror also hosts ubuntu and a few others which run at the same time, if this info is needed.
Right now the cron job just SH?s a script containing a screen call with the rsync command in it. And I would like to keep it this way. At least that sh script. What im really looking for is a script that checks for the lockfile if its running. And then if its not SH?s the script. I would have the cron job run that script, and that script would run my sh script called centos.sh located in /root/
On my mirror, I have a separate script for each project I mirror. My update-centos.sh script looks like this:
### 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 ###
### Remove lock file /bin/rm -rf /tmp/update-centos.lock ### end update-centos.sh
I also have /etc/rc.local remove the lock files for all my scripts (in case I reboot during a run): # /etc/rc.local /bin/rm /tmp/update.lock >/dev/null 2>&1
Hope this helps. DR
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
Ok I think I got it, just one more thing. If I make the lock file myself with the date >> /tmp/update-centos.lock file it works and doesn't let another one spawn. However. If its not there its not creating it. Do I have the else part in the right spot?
#!/bin/bash
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 # No, it doesn't exist. # Create the lock file so another run won't start. else 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-updates --progress rsync://linux.mirrors.es.net/centos/ /home/www/centos DATE=`date` echo "$DATE with mirror linux.mirrors.es.net" > /home/www/txt/centostime.txt
# Now we're done. Remove the lock file. /bin/rm -rf /tmp/update-centos.lock
-----Original Message----- From: centos-mirror-bounces@centos.org [mailto:centos-mirror-bounces@centos.org] On Behalf Of David Richardson Sent: Wednesday, August 05, 2009 6:30 PM To: Mailing list for CentOS mirrors. Subject: Re: [CentOS-mirror] lock files
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
On Wed, 5 Aug 2009, Nick Olsen wrote:
Ok I think I got it, just one more thing. If I make the lock file myself with the date >> /tmp/update-centos.lock file it works and doesn't let another one spawn. However. If its not there its not creating it. Do I have the else part in the right spot?
#!/bin/bash
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 # No, it doesn't exist. # Create the lock file so another run won't start. else 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-updates --progress rsync://linux.mirrors.es.net/centos/ /home/www/centos DATE=`date` echo "$DATE with mirror linux.mirrors.es.net" > /home/www/txt/centostime.txt
# Now we're done. Remove the lock file. /bin/rm -rf /tmp/update-centos.lock
Hmm. What happens if you remove screen from the picture (that is, have the script run the rsync directly)?
DR
Lesson learned for all, When you run the script it just runs the screen command, which the rsync runs in. So the script completes because its run the screen command, Thus running the rm -rf on the lock file :) thanks for all the help David.
-----Original Message----- From: centos-mirror-bounces@centos.org [mailto:centos-mirror-bounces@centos.org] On Behalf Of David Richardson Sent: Wednesday, August 05, 2009 7:20 PM To: Mailing list for CentOS mirrors. Subject: Re: [CentOS-mirror] lock files
On Wed, 5 Aug 2009, Nick Olsen wrote:
Ok I think I got it, just one more thing. If I make the lock file
myself
with the date >> /tmp/update-centos.lock file it works and doesn't let another one spawn. However. If its not there its not creating it. Do I have the else part in the right spot?
#!/bin/bash
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 # No, it doesn't exist. # Create the lock file so another run won't start. else 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-updates --progress rsync://linux.mirrors.es.net/centos/ /home/www/centos DATE=`date` echo "$DATE with mirror linux.mirrors.es.net" > /home/www/txt/centostime.txt
# Now we're done. Remove the lock file. /bin/rm -rf /tmp/update-centos.lock
Hmm. What happens if you remove screen from the picture (that is, have the script run the rsync directly)?
DR