Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting "cat: write error: Broken pipe" rarely and not always

I am running some scripts with commands having cat pipelined with grep like:

cat file.txt | grep "pattern"

Most of the times there are no problems. But sometimes I get below error:

cat: write error: Broken pipe

So how do I find out when the command is causing this problem and why?

like image 620
Vishal-Lia Avatar asked Apr 05 '18 05:04

Vishal-Lia


People also ask

What causes broken pipe error?

This error generally means means that the data stopped flowing to us and we were unable to start the transfer again. Often times this is caused by a wireless internet connection with fluctuating signal strength, a firewall or other security software.

What does pipe error mean?

A broken Pipe Error is generally an Input/Output Error, which is occurred at the Linux System level. The error has occurred during the reading and writing of the files and it mainly occurs during the operations of the files.


1 Answers

The reason is because the pipe is closed by grep when it still has some data to be read from cat. The signal SIGPIPE is caught by cat and it exits.

What usually happens in a pipeline is the shell runs cat in one process and grep in another. The stdout of cat is connected to the write-end of the pipe and stdin of grep to the read end. What happened was grep hit a pattern search that did not exist and exited immediately causing the read end of the pipe to be closed, which cat does not like since it has some more data to be write out to the pipe. Since the write actions happens to an other which has been closed other end, SIGPIPE is caught by the cat on which it immediately exits.

For such a trivial case, you could remove the pipeline usage altogether and run it as grep "pattern" file.txt when the file's contents are made available over the stdin of grep on which it could read from.

like image 184
Inian Avatar answered Sep 28 '22 03:09

Inian