flock
is one of the ways of interlocking operations. The utility is part of the util-linux toolset and is only available for Linux. Other utilities, available across a wider range of platforms, are based around Daniel J. Bernstein's setlock
utility from his daemontools package:
These tools operate with a slightly different paradigm to that used in M. Kurenkov's answer (one that flock
can also employ, but does not in that answer). One invokes the setlock
program to chain load to the command that must be interlocked. setlock
itself opens and locks the lock file, and leaves a file descriptor for it open in its process. The lock persists for as long as that process does (unless the subsequent command chained to explicitly releases the lock by finding and closing the open file descriptor).
For the case in the question one must interlock the command that produces the output line, being aware that this invokes an external echo
in place of a shell built-in echo
command:
setlock mylockfile echo "$RESULT" >> ./$TEMP_DIR/$OUT_FILE
In this case it is not necessary to interlock opening the output file in append mode. If it were, one would have to open that file within the lock, which necessitates either using programs like fdredir
/redirfd
:
setlock mylockfile fdredir --append 1 "./$TEMP_DIR/$OUT_FILE" echo "$RESULT"
which one can turn into a shell function if one wants:
outfile() { setlock mylockfile fdredir --append 1 "./$TEMP_DIR/$OUT_FILE" "$@" ; }
[…]
outfile echo "$RESULT"
or sticking with the shell syntax and having it interpreted by a second shell running under the interlock, requiring some non-trivial quoting if one's shell variables are not exported as environment variables:
setlock mylockfile sh -c 'echo '"$RESULT"' >> "./'$TEMP_DIR'/'$OUT_FILE'"'
This of course generalizes to things other than writing to output files:
setlock mylockfile sh -c '… interlocked ; stuff …'
\
pwd``; you can just use a dot (.
). Also you should quote that whole file name since it includes variables. – Wildcard Apr 05 '16 at 17:51pwd
earlier in the script to notify the user about the current working directory and also write entries in a log file. looking over FIFOs now. – Sebi Apr 05 '16 at 17:59mkfifo
and not exactly introductory level. – Wildcard Apr 05 '16 at 18:02