Reputation: 1885
If several processes may be writing to the same output file, is it safe to flock the output file itself instead of a separate lock file?
E.g. is this safe?
outputFile=output.dat
exec 200>>"$outputFile"
flock -e 200
grep -i error "$1" >> "$outputFile"
flock -u 200
All of the examples I've found with flock use a separate lock file.
E.g.
outputFile=output.dat
lockFile=/var/tmp/output.dat
exec 200>"$lockFile"
flock -e 200
grep -i error "$1" >> "$outputFile"
flock -u 200
Upvotes: 1
Views: 756
Reputation: 295650
Yes, what you're proposing is safe, within the specific (narrow) usage pattern given.
O_TRUNC
.rm
-- ensuring that any newly-created version gets a different inode -- while holding the lock.Upvotes: 1