Bash Named Pipe FIFO Producer-Consumer Pipeline Prompt
Decouple a data producer from one or more consumers across separate processes using a mkfifo named pipe, with correct open/blocking semantics and cleanup
- Target user
- SREs and automation engineers building lightweight inter-process streaming without a message broker
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior automation engineer who specializes in Unix named pipes (FIFOs) and shell concurrency patterns. I will provide: - What the producer emits and how often (e.g. log lines, job ids, metrics) - How many consumers I need and whether each line should go to one consumer or be fanned out - Where the FIFO should live and the user/permissions constraints Your job: 1. **Create the FIFO** — `mkfifo` with an explicit mode, placed in a private directory (`mktemp -d`), and explain why a FIFO differs from a regular file. 2. **Explain blocking on open** — clarify that opening a FIFO for read blocks until a writer appears (and vice versa) and how to avoid a startup deadlock. 3. **Wire the producer** — write to the FIFO with line-buffered output, handling `SIGPIPE`/`EPIPE` when no consumer is attached. 4. **Wire the consumer(s)** — read loop with `while IFS= read -r line; do ... done < "$fifo"`, and show the single-consumer vs. fan-out distinction (one FIFO ≠ broadcast). 5. **Manage lifecycle** — start order, backgrounding with `&`, and how an unexpected reader EOF closes the pipe. 6. **Clean up** — `trap` to `rm -f` the FIFO and kill background readers on EXIT/INT/TERM. Output as: a runnable producer script and consumer script (two code blocks) plus a 3-line "how to launch" note. Warn explicitly that a FIFO delivers each byte to exactly ONE reader — do not present it as a pub/sub broadcast mechanism.