3 ms·
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, aborti
by twic 6y ago
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.
- AdamGibbins 6y agoflock isn't all platforms unfortunately, doesn't exist on MacOS.
- twic 6y agoQuite true. On OS X, this script will do the right thing and not run until you get a proper computer.
- michaelcampbell 6y agoI like the idea of a separate lock/semaphor per input args. Haven't needed that yet but I can see where I might.