named pipes apparently not working/responding?

2

I am trying to use named pipes as a convenient way to preprocess input on the fly for pipelines that sadly do not accept data from stdin directly.

everywhere I look for info I get basically the same gist: Named pipes should be dead simple to use.

the gist is mostly that the following should work:

mkfifo mynamedpipe echo "is this working?" > mynamedpipe cat mynamedpipe

when i run mkfifo mynamedpipe, the pipe is successfully created and visible with ls *.

But even after i grant myself write permission to that pipe, when i try to run echo "whatever" > mynamedpipe nothing happens and the terminal just hangs until I kill the process with ctrl+c.

I have this problem on my local linux machine (Ubuntu 14.04.5 LTS) as well as on a public server (Red Hat Enterprise Linux 7), and in zsh as well as in bash.

What am I doing wrong here?

jov14

Posted 2018-09-06T16:45:58.643

Reputation: 53

1

try cat < mynamedpipe. also note this quote from https://www.linuxjournal.com/article/2156 : "If you watch closely, you'll notice that the first command you run appears to hang. This happens because the other end of the pipe is not yet connected, and so the kernel suspends the first process until the second process opens the pipe. In Unix jargon, the process is said to be “blocked”, since it is waiting for something to happen."

– Frank Thomas – 2018-09-06T16:57:06.610

Thanks that clears things up for me. But it is annoying that virtually every tutorial on this matter (or at least all of the most prominent google hits) describes exactly the basic steps I posted above, stating that they should work "as-is", without ever addressing that problem. Is this something that changed in recent times? – jov14 – 2018-09-06T17:28:14.673

You should use the reader (cat mynamedpipe) first. Which means you need two terminals. Also, this smells like an XY question. Please explain the actual problem you are trying to solve: What are those "pipelines that sadly do not accept data from stdin directly" exactly?

– dirkt – 2018-09-07T06:10:39.553

Answers

1

This post seems to relate to your problem : Cat to named pipe causes hang.

The relevant remarks are :

  • You need to have something reading from the FIFO
  • Ensure that the pipe is created with a large enough buffer or readers are fast enough to avoid blocking
  • You need to assign the pipe to a file descriptor, as in :

    exec 3<>/tmp/stream_pipe
    

harrymc

Posted 2018-09-06T16:45:58.643

Reputation: 306 093

Thanks for the info. Sadly that seems to mean that named pipes are not an alternative for me, as that poster also states that trying to change the buffer size changed nothing for him. I was looking for an alternative to plug in with downstream tools that do not read from stdin, so the pipe is not necessarily being consumed as it is being written. Makes sense for pipes to behave that way though. So back to writing to and reading from intermediate physical temp-files for me... – jov14 – 2018-09-06T17:34:31.193

Named pipes are nice but unfortunately not a solution for potentially very large data. – harrymc – 2018-09-06T17:42:15.250