Perhaps that example code would clearer if written:
(
if ! flock -w 1 200 ; then
echo >&2 "Another process is still running..."
exit
fi
RunLocked
) 200> ${lockdir}/mirror.centos.lock
Or
exec 200> ${lockdir}/mirror.centos.lock
if ! flock -w 1 200 ; then
echo >&2 "Another process is still running..."
exit
fi
RunLocked
Or for a more succinct version:
( flock -w 1 200 && RunLocked ) 200> ${lockdir}/mirror.centos.lock
(Point is: referencing “$?
” is almost never needed, and usually obfuscatory.)
From: centos-mirror-bounces@centos.org [mailto:centos-mirror-bounces@centos.org] On Behalf Of João Carlos Mendes Luís
Sent: 13 July 2011 03:29
To: Mailing list for CentOS mirrors.
Subject: Re: [CentOS-mirror] I see all your new mirrors ...
Using "touch" and "test -e" as a lock test is not safe. If the shell dies for any reason, the lock file will get stale.
If you sync station is linux, a safer option is to use the flock(1) program:
NAME
flock - Manage locks from shell scripts
SYNOPSIS
flock [-sxon] [-w timeout] lockfile [-c] command...
flock [-sxon] [-w timeout] lockdir [-c] command...
flock [-sxun] [-w timeout] fd
Here is the lock part of my mirror script:
RunLocked()
{
....
}
(
flock -w 1 200
if [ "$?" -ne 0 ] ; then
echo "Error, lock is already taken..."
exit
else
RunLocked
fi
) 200> ${lockdir}/mirror.centos.lock
Jonny
--
João Carlos Mendes Luís - Computer & Networking Engineer
jonny@jonny.eng.br
On 07/12/2011 11:56 AM, Adam wrote:
admin wrote:
Yes It would be great . I'm still rsync centos repo.
Could you provide me bash script to rsync with file locking ?
Attached is a bash locking rsync script. This script is pretty informative and intelligent. It verifies that the time stamp of the server you are syncing form is newer then yours before it syncs. Hence it shouldn't ever delete files you have already downloaded if you hit an old mirror.
Adam
_______________________________________________
CentOS-mirror mailing list
CentOS-mirror@centos.org
http://lists.centos.org/mailman/listinfo/centos-mirror