baumi's blog

baumi's personal blog … Linux, OS X, Windows, Random things, …

Bash: preventing multiple concurrent executions

I’ve found all kinds of solutions achieving this, but this is the nicest solution i’ve seen so far:

#!/bin/bash
exec 200<$0
flock -n 200 || exit 1

echo "sleep begin";
sleep 10
echo "sleep end";

found on ... https://stackoverflow.com/questions/7057234/bash-flock-exit-if-cant-acquire-lock/7305448#7305448

See also: https://blog.famzah.net/2013/07/31/using-flock-in-bash-without-invoking-a-subshell/

Comments are currently closed.