The following lock mechanism is used for preventing a cron
job from running concurrently:
#!/bin/bash
echo "Before critical section"
(
flock -e 200
echo "In critical section"
sleep 5
) 200>/tmp/blah.lockfile
echo "After critical section"
When running two instances together, the later waits until the first finishes, and then runs. This can cause backlogs of scripts waiting to run.
How do I alter this script so that if flock
can't acquire the lock, it terminates the script? I've tried -n
without success.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…