Using lockfile to create a semaphore in bash scripts
Sometimes you want to limit the number of times a script can run in parallel. You can do so by using lockfile
(part of procmail). For example: want to make sure a script only has one instance? Simply add
lockfile -r 0 "<lock file name>" || exit 1
to the start of your script. Retry count, sleep time, etc are configurable. The -!
flag inverts the exit status, making in possible to use lockfile
as break conditions in a while
loops with bash.