Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Flock Explanation

151 views
Skip to first unread message

Craig A. Adams

unread,
May 23, 2012, 12:30:02 PM5/23/12
to
Hi,

In my thread about automated backup, flock was recommended as a locking
solution.

I am trying to get my head around flock, so I am hoping someone can explain.

A typical flock example seems to read as follows:

(
flock -x -n 200
if [ $? != "0" ]; then
echo "Unable to obtain exclusive lock. Another backup process
is running."
exit 1
fi
## Script code here ##

) 200>/data/backups/disk-1.backup.lock

Please correct my assumptions and explain where I am wrong.

1. The brackets ( ) act as a wrapper for the entire script.

2. -The x means exclusive lock is requested.

3. The -n means that the script should fail if an exclusive lock cannot
be immediately obtained (versus waiting for a lock).

4. I have no idea what the 200 is. As a blind guess a locking period in
seconds or is it an access mode?

5. The "if [ $? != "0" ]" statement tests if there is already an
exclusive lock on the file and exits the entire script if there is.

6. The "if ... fi" is the entire locking test and if passed the script
code is executed.

7. The last line is part of the wrapper assumed in 1 above, actually
creating the lock file using the > redirector. But what is the 200 again?

Kindest Regards

Craig A. Adams


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/4FBD0F7D...@iafrica.com

rbmj

unread,
May 23, 2012, 1:10:01 PM5/23/12
to
On 05/23/2012 12:25 PM, Craig A. Adams wrote:
> 4. I have no idea what the 200 is. As a blind guess a locking period
> in seconds or is it an access mode?

I believe that the 200 is a file descriptor number. File descriptors
are numbers that identify a certain file that is open for
reading/writing. For example, standard input is 0 and standard output is 1.

> 5. The "if [ $? != "0" ]" statement tests if there is already an
> exclusive lock on the file and exits the entire script if there is.

More specifically, it tests if flock exited successfully (i.e. there is
a lock, in this case). It then exits the subshell (the section
surrounded by () parens).

> 7. The last line is part of the wrapper assumed in 1 above, actually
> creating the lock file using the > redirector. But what is the 200 again?

The > redirector is redirecting file descriptor 200 to the file you are
specifying. In this case, I think that the choice of 200 is arbitrary.
As long as you redirect the same file descriptor that you pass to flock,
then the number shouldn't matter (though don't choose stdout :D)

--
rbmj


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/4FBD183F...@verizon.net

Craig A. Adams

unread,
May 23, 2012, 2:40:01 PM5/23/12
to
Thank you for the help rbmj.

On 23/05/2012 19:02 PM, rbmj wrote:
> a lock, in this case). It then exits the subshell (the section
> surrounded by () parens).

What is the effect of exit codes in the script code section? Does an
exit 0(1,2...) exit just the subshell or the entire script? I am worried
by a lock being left in place by one of these.

> The > redirector is redirecting file descriptor 200 to the file you are
> specifying. In this case, I think that the choice of 200 is arbitrary.
> As long as you redirect the same file descriptor that you pass to flock,
> then the number shouldn't matter (though don't choose stdout :D)

So it is not actually creating the lock file? So I must make sure the
lock file exists in advance?

Kindest Regards

Craig A. Adams


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/4FBD2D1C...@iafrica.com

Kushal Kumaran

unread,
May 24, 2012, 1:10:01 AM5/24/12
to
On Thu, May 24, 2012 at 12:01 AM, Craig A. Adams <cra...@iafrica.com> wrote:
> Thank you for the help rbmj.
>
>
> On 23/05/2012 19:02 PM, rbmj wrote:
>>
>> a lock, in this case). It then exits the subshell (the section
>> surrounded by () parens).
>
>
> What is the effect of exit codes in the script code section? Does an exit
> 0(1,2...) exit just the subshell or the entire script? I am worried by a
> lock being left in place by one of these.
>
>
>> The > redirector is redirecting file descriptor 200 to the file you are
>> specifying. In this case, I think that the choice of 200 is arbitrary.
>> As long as you redirect the same file descriptor that you pass to flock,
>> then the number shouldn't matter (though don't choose stdout :D)
>
>
> So it is not actually creating the lock file? So I must make sure the lock
> file exists in advance?
>

If you use the N> FILENAME syntax, the shell will create the file and
make descriptor N refer to the named file. You don't have to create
the file yourself. Details of this kind of redirection are in the
bash manual, if you're interested.

Understanding this kind of usage of flock requires an understanding of
the inheritance of descriptors from parent process to child process.
It is by no means complicated, but we might be getting rather
off-topic here.

For your immediate needs, you can use one of the other forms of the
flock command.

Just replace your backup command:

/script/backup arg1 arg2

with:

flock -xn /tmp/backup-lock /script/backup arg1 arg2

Don't worry about locks being left behind. When your backup script
ends, the flock command also ends, releasing its lock on
/tmp/backup-lock. The file is automatically created the first time,
but will not be deleted. It's a 0-byte file, so this should not be
terribly worrisome.

--
regards,
kushal


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/CAH8GtdNqNJDEg20dUHy0vr5HrkpDym3u7GszAv9=vz0R+...@mail.gmail.com

Craig A. Adams

unread,
May 24, 2012, 1:30:02 AM5/24/12
to
Hi Kushal,

Thanks for the assist.

On 24/05/2012 07:05 AM, Kushal Kumaran wrote:
> If you use the N> FILENAME syntax, the shell will create the file and
> make descriptor N refer to the named file. You don't have to create
> the file yourself. Details of this kind of redirection are in the
> bash manual, if you're interested.

Thank you. In the usual context I would have expected the > to create
the file. I just was not sure.

> Just replace your backup command:
>
> /script/backup arg1 arg2
>
> with:
>
> flock -xn /tmp/backup-lock /script/backup arg1 arg2

This query actually stemmed from my backup thread query (Automatic
Backup Script Help), where I have a udev rule kick off the backup script.

In order to prevent the suspension of udev operations I intend to use a
staging script, which originally read as follows...

/data/backups/scripts/start-disk-1.sh
#!/bin/bash
echo /data/backups/scripts/backup-disk-1.sh | at now + 1 minute

From your info, I guess I can now use...

/data/backups/scripts/start-disk-1.sh
#!/bin/bash
echo flock -xn /tmp/disk-1-lock /data/backups/scripts/backup-disk-1.sh |
at now + 1 minute

Kindest Regards

Craig A. Adams


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Archive: http://lists.debian.org/4FBDC5EC...@iafrica.com
0 new messages