Bob's Notepad

Notes on projects I have done and things I have learned saved for my reference and for the world to share

Tuesday, November 20, 2007

Using lock files in a script

When dealing with automated rsync backup scripts you sometimes run in to the issue of a large file taking longer than expected and cron launching another rsync -- eventually spiraling things down. The way to resolve this is create a lock file so that your script doesn't launch again if it is already running. Just put the following in your script under the #!/bin/sh statements:

LOCKFILE="/var/lock/rsync/lockfile"
if [ -f $LOCKFILE ]
then
echo Lock file exists...Exiting...
exit 0
else
touch $LOCKFILE
fi


This declares what the lock file is (you can make the file anything as long as the user running the script has access to read and write). Then it checks to see if the lockfile exists and exits if it does. If it doesnt exist, then it creates it.

All thats left is to add the following to the end of the script so that it cleanly deletes the lockfile after running:

rm -f $LOCKFILE
exit 0


Thanks to @linuxchic for helping out :)

Labels: , ,

Reference Link


0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home