How to Fix ‘Broken Pipe’ Error in Linux Terminal

Executing pipeline commands, streaming network transfers, or managing remote SSH sessions in the Linux terminal can unexpectedly abort with the message: Write error: Broken pipe, cat: write error: Broken pipe, or simply packet_write_wait: Connection to [host] port 22: Broken pipe. In Unix-like operating systems, pipes (represented by the vertical bar |) stream output data from one process into the input buffer of another. When the receiving program closes its input stream prematurely while the sending program continues pushing data, the Linux kernel halts the process by issuing a SIGPIPE (Signal 13) error.

Diagnosing and eliminating broken pipe failures requires managing process buffers in Bash, adjusting SSH keepalive parameters, and handling SIGPIPE exceptions in terminal scripts.

What Causes Broken Pipe Errors in Linux?

A broken pipe indicates an abrupt termination of a unidirectional communication channel. Common triggers include:

  • Early Termination of Piped Commands: Piping extensive output to utilities like head or grep -q causes the downstream process to exit as soon as its match condition is satisfied. When the upstream utility (such as cat, find, or yes) attempts to send more data into the closed FIFO buffer, the kernel triggers SIGPIPE.
  • SSH Network Timeout or NAT Keepalive Drops: Home routers, firewalls, and stateful NAT gateways drop idle TCP connections on port 22 after 5 to 15 minutes of inactivity. When the client wakes up and attempts a write handshake, the disconnected socket triggers packet_write_wait: Broken pipe.
  • Premature Client Disconnects in Web and Application Servers: Web servers (like Nginx, Apache, or Python Flask) streaming large file payloads encounter broken pipes when users close browser tabs or cancel downloads before completion.
  • Exhausted System File Descriptors: The operating system process reaches its maximum allocated open file descriptors (ulimit -n), causing buffer write streams to collapse.

Method 1: Prevent SSH ‘packet_write_wait: Broken pipe’ Disconnections

If your remote server SSH sessions frequently freeze and terminate with broken pipe errors after a few minutes of idle time, configure client-side TCP keepalive heartbeats:

  1. Open or create your local user SSH configuration file:
    nano ~/.ssh/config
  2. Add the following parameters to ensure your terminal sends keepalive packets every 60 seconds:
    Host *
        ServerAliveInterval 60
        ServerAliveCountMax 3
        TCPKeepAlive yes
  3. Save and close the file (Ctrl + O, Enter, Ctrl + X).
  4. Ensure secure file permissions:
    chmod 600 ~/.ssh/config
  5. Alternatively, if you manage the remote server, edit the server-side configuration at /etc/ssh/sshd_config:
    sudo nano /etc/ssh/sshd_config

    Add or uncomment: ClientAliveInterval 60 and ClientAliveCountMax 3, then restart SSH with sudo systemctl restart ssh.

Method 2: Handle SIGPIPE Cleanly in Bash Command Pipelines

When piping commands where the receiving process exits early (e.g., cat hugefile.log | head -n 10), suppress standard error noise or manage process exit statuses:

  1. By default, Bash evaluates pipeline success based on the last command. If you have set -e or set -o pipefail enabled in your automated shell scripts, SIGPIPE returns exit code 141 (128 + Signal 13), causing the script to abort.
  2. Handle SIGPIPE safely by catching or trapping the signal in shell automation:
    # Catch and ignore SIGPIPE exit code 141 in critical script workflows:
    some_large_command | head -n 5 || if [ $? -eq 141 ]; then true; else exit $?; fi
  3. Alternatively, redirect standard error to /dev/null if you only care about extracted head output:
    find / -name "*.conf" 2>/dev/null | head -n 10

Method 3: Manage Python and Node.js Stream Handling

When authoring command-line tools that stream data into Linux pipes, handle SIGPIPE gracefully rather than dumping tracebacks:

# Example graceful SIGPIPE handling in Python:
import sys
from signal import signal, SIGPIPE, SIG_DFL

# Reset SIGPIPE to default Unix handling to exit silently without Python traceback
signal(SIGPIPE, SIG_DFL)

Configuring default signal behavior ensures your utility exits cleanly when an end-user pipes output into less, head, or q.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.