I want to use a queue in Bash, which one process (eg. one Bash script) will be writing to and several other processes will be reading from.
I want to do this:
writer:
# block if another process is reading or writing,
# then lock the queue using an exclusive lock
lock -ex queue
echo "$message" >> queue
unlock queue
reader:
# wait until there is an item in the queue
# and then lock the queue using a shared lock
wait_while_empty_and_lock -sh queue
read -r message < <(tail -n+1 queue)
(do stuff...)
unlock queue
Preferably, I would like to use Bash + standard / commonly available Linux/UNIX utilities (not Redis et al.). I have considered several approaches involving flock
, several FIFOs, etc., but I couldn't come up with satisfactory results. Is there a simple way to do this in Bash/Linux/UNIX, or do I have to resort to third-party tools such as Redis?