I have a set of shell scripts that perform backups on my system using truecrypt and rsync. I want to avoid any possibility of these various scripts running at the same time.
They use flock
in a subshell, as described in the manpage:
(
flock -n 9 || exit 1
# ... commands executed under lock ...
) 9>/var/lock/mylockfile
However, the lock is always failing to acquire for subsequent runs (with exit status 1).
Yet fuser /var/lock/mylockfile
and lsof /var/lock/mylockfile
show nothing.
Now, if I add the command flock -u
to manually unlock, like this:
(
flock -n 9 || exit 1
# ... commands executed under lock ...
flock -u 9
) 9>/var/lock/mylockfile
Then the scripts work.
Am I using flock
correctly? Is it safe to call flock -u
inside the block?
FYI The troublesome scripts are the ones that call truecrypt within the command block.
The problem is that you're running TrueCrypt in the background, keeping the fd open. You should close the fd to prevent background processes from hanging on to the lock.
In lieu of your actual code, here's a test case:
foo() {
(
flock -n 9 && echo "ok" || { echo failed; exit 1; }
sleep 10 &
) 9> lock
}
foo; foo
# Output:
# ok
# failed
sleep
is forked with fd 9 open, causing the lock to be kept. Let's close fd 9 when backgrounding the process:
foo() {
(
flock -n 9 && echo "ok" || { echo failed; exit 1; }
sleep 10 9>&- &
# ^-------- Right here
) 9> lock
}
foo; foo
# Output:
# ok
# ok
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With