Protecting a bash script against concurrent execution.
This is very simple and easy in bash and probably fine in sh (not tested). The lockfile is never deleted but that is not an issue.
exec 9>>mylockfile
if ! flock --exclusive --nonblock 9; then
echo "$(date -u +"%FT%T.%3NZ") $$: scriptname will wait for lock"