News for hackers. Stuff that matters.
Re:Minimal safe Bash script template by twic on Tuesday December 15, 2020 @08:17AM (#25429986)
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.

Re:Minimal safe Bash script template by michaelcampbell on Thursday December 17, 2020 @09:55AM (#25456087)
I like the idea of a separate lock/semaphor per input args. Haven't needed that yet but I can see where I might.
Re:Minimal safe Bash script template by AdamGibbins on Tuesday December 15, 2020 @05:54PM (#25436447)
flock isn't all platforms unfortunately, doesn't exist on MacOS.
Re:Minimal safe Bash script template by twic on Wednesday December 16, 2020 @02:15AM (#25439919)
Quite true. On OS X, this script will do the right thing and not run until you get a proper computer.