Broken Pipe
Linux Linux
Severity: MinorWhat Does This Error Mean?
A 'Broken pipe' error in Linux means a program tried to write data to a connection or pipe, but the other end had already closed. You often see this in SSH sessions that time out, scripts using pipes between commands, or network connections that drop unexpectedly. In most cases it is not a critical error — the program simply stopped because there was nowhere to send its output.
Affected Models
- Ubuntu
- Debian
- Fedora
- CentOS
- Arch Linux
- Linux Mint
- openSUSE
Common Causes
- An SSH session timed out while a command was still running, closing the pipe
- A script piped output to a command like head or grep that closed before reading all the data
- A network connection dropped while a program was writing to a socket
- A reading process (the one receiving pipe data) exited before reading all the data
- The terminal window was closed while a long-running command was still outputting data
How to Fix It
-
For SSH sessions, keep the connection alive. Edit ~/.ssh/config on your local machine and add: ServerAliveInterval 60 and ServerAliveCountMax 3. This sends keepalive messages every 60 seconds.
SSH connections time out after inactivity. Keepalive messages prevent the server from closing an idle connection.
-
Use screen or tmux for long-running commands. Install tmux: sudo apt install tmux. Start a session: tmux new -s mysession. Run your command. Detach: Ctrl+B then D. Reattach later: tmux attach -t mysession.
tmux and screen keep your session running even if the SSH connection drops. When you reconnect, you reattach to the same running session.
-
Use nohup for commands that should survive SSH disconnection. Run: nohup [your command] &. Output goes to nohup.out instead of the terminal.
nohup ignores the hangup signal (SIGHUP) that terminates programs when an SSH session ends. The & puts it in the background.
-
Ignore broken pipe in scripts. If a script exits with 'Broken pipe' when using head or grep, this is normal — those commands close the pipe after getting what they need. Add 2>/dev/null to suppress the error message.
For example: some_command | head -10 2>/dev/null. The head command closes the pipe after reading 10 lines, causing the writing side to report a broken pipe.
-
Increase server SSH timeout. On the SSH server, edit /etc/ssh/sshd_config and add: ClientAliveInterval 60 and ClientAliveCountMax 3. Then: sudo systemctl restart sshd.
This configures the server to send keepalive probes to clients, preventing it from closing connections that appear inactive.
When to Call a Professional
Broken pipe errors almost never require professional help. They are a normal part of how Linux programs communicate. Only investigate further if the broken pipe is causing data loss or a critical service to fail.
Frequently Asked Questions
A script failed partway through with 'Broken pipe.' Did the data get corrupted?
Possibly — it depends on what the script was doing. If the script was writing to a file and the broken pipe interrupted it, the file might be incomplete. Check the file size and contents. If it was a read-only operation (like searching or displaying data), no data corruption occurred.
Why do I only get broken pipe errors when using SSH but not locally?
Because SSH adds an extra connection layer that can time out. Locally, pipes are purely in-memory connections that do not time out. Over SSH, the network connection itself can drop, taking the pipe with it.
Can broken pipe errors affect a running web server?
Broken pipe errors in web servers are normal and expected. When a browser closes a connection before the server finishes sending a response, the server gets a broken pipe error. All major web servers (Apache, Nginx) handle these gracefully and simply log them at debug level. They do not affect other requests or the server's stability.