Read to the end before screaming. I do this:
exec >>$log_file 2>&1
if ! flock --exclusive --nonblock 1
then
log "unable to get exclusive lock, aborting"
exit 1
fi
Which redirects standard output to a log file, then tries to get an exclusive lock on it. The lock is created by the flock subprocess, but continues to exist until the script exits and closes that file descriptor.This means there can only be one process writing to a given log file at a time. For me, that is the right granularity of locking - i have several cron jobs running this script, but with different arguments, and writing to different log files.