I want to use lockfile functionality for a bash script that will run in parallel. The problem is that in a functionality as in the script below, a starvation can happen in case that the first one to lock is called immediately again. It seems that lockfile
is polling with a big enough timeout to allow other processes to acquire the lock.
Is that true? How should that be handled?
lockfile test.lock
echo "TESTING $$"
sleep 10
rm test.lock
For example:
Two shells :
SH1, SH2 run the script together,
SH1 acquires lock, SH2 is blocked.
PROBLEM:
When SH1 is done and removes the lock, SH2 is still blocked (for about a second...), and if SH1 runs the script in a loop, SH2 will be blocked permanently.
lockfile test$$.lock
can help – Costas Jan 19 '15 at 13:56lockfile
. It will hang until the lock is available (usig theflock
system call) rather than polling. – Mark Plotnick Jan 19 '15 at 21:11