How to Merge Multiple Named Pipes into a Single Input Stream in Linux: A Technical Guide


2 views

When working with Linux pipes and inter-process communication, we often encounter situations where we need to combine multiple input sources. While the tee command helps split output streams, merging inputs requires different approaches.

Here are three effective methods to combine named pipes:

# Method 1: Using cat with process substitution
cat <(pipe1) <(pipe2) | processing_command

# Method 2: Explicit named pipe concatenation
mkfifo combined_pipe
cat pipe1 pipe2 > combined_pipe &
processor_command < combined_pipe

# Method 3: Using tee with multiple inputs
cat pipe1 | tee pipe2 | processing_command

Named pipes (FIFOs) require special handling:

# Create named pipes
mkfifo input1 input2

# Writer processes (in separate terminals)
echo "Data from source 1" > input1 &
echo "Data from source 2" > input2 &

# Reader process
cat input1 input2 | your_processing_script

For more complex scenarios:

# Prioritized merging with timestamp prefixes
paste <(while read line; do echo "[1] $line"; done < pipe1) \
      <(while read line; do echo "[2] $line"; done < pipe2) | \
      sort -k2

Combine multiple log streams:

# Create pipes
mkfifo /var/log/pipe1 /var/log/pipe2

# Start log producers
tail -f /var/log/syslog > /var/log/pipe1 &
tail -f /var/log/nginx/access.log > /var/log/pipe2 &

# Combined processor
cat /var/log/pipe1 /var/log/pipe2 | \
    grep -v "debug" | \
    logger -t "combined_logs"

When merging high-volume streams:

  • Use buffers (e.g., stdbuf) to prevent blocking
  • Consider socat for TCP-based pipe merging
  • Monitor with pv to track throughput

Watch for these pitfalls:

# Check for hanging processes
lsof | grep FIFO

# Verify pipe permissions
ls -l /path/to/your/pipe

# Test pipe functionality
echo "test" > pipe1 & cat pipe1

When working with Linux pipelines, we often need to combine multiple input sources. While the tee command excels at splitting streams, merging requires different approaches.

For sequential processing:

mkfifo pipe1 pipe2
command1 > pipe1 &
command2 > pipe2 &
cat pipe1 pipe2 | processing_command

For simultaneous processing using dd:

mkfifo combined
dd if=pipe1 of=combined &
dd if=pipe2 of=combined &
processing_command < combined

The socat tool provides robust stream merging:

socat -u - GOPEN:pipe1 -u - GOPEN:pipe2 | processing_command

Merge logs from two services:

mkfifo /var/log/service1.fifo /var/log/service2.fifo
service1 --log-fifo=/var/log/service1.fifo &
service2 --log-fifo=/var/log/service2.fifo &
socat -u - GOPEN:/var/log/service1.fifo -u - GOPEN:/var/log/service2.fifo | \
    grep "ERROR" | \
    awk '{print strftime("%Y-%m-%d %H:%M:%S"), $0}'

When merging high-volume streams:

  • Buffer sizes matter - adjust with --buffer-size in socat
  • Monitor with pv to identify bottlenecks
  • Consider using RAM disks for temporary FIFOs

Diagnose problems with:

strace -f -e trace=open,read,write socat [arguments]

Watch for deadlocks when both pipes block waiting for readers.