1

There's a service that pipes it's output to a named pipe, which I create with mkfifo name.

The service pauses if there's no reader in pipe. How to make it to keep piping the data even if there's no reader?

The reader(s) may (or may not) use the pipe later.

Edit: No buffer is needed. Readers will start reading from where they use the pipe. Data before that is lost. And that's OK. Consider it's a video stream.

james hofer
  • 107
  • 7
  • 1
    AFAIK you're always going to be limited by buffer size - see for example [Buffering (named) pipe in GNU OS](https://unix.stackexchange.com/questions/164290/buffering-named-pipe-in-gnu-os) and [Non-blocking buffered named pipe?](https://unix.stackexchange.com/questions/23488/non-blocking-buffered-named-pipe) – steeldriver Feb 24 '23 at 15:19
  • I don't need to buffer data. Readers will lose what they lose. But that's no problem. They get the data from where they start reading. – james hofer Feb 24 '23 at 15:44
  • I think I should attach some sort of dummy reader to the pipe as soon as it's created, so the process won't pause, isn't it? – james hofer Feb 24 '23 at 15:45
  • The first link in @steeldriver s comment explains how to prevent the writer being blocked by no reader. I tried it and it works great. – Doug Smythies Feb 24 '23 at 16:06
  • @DougSmythies I checked the link. It fills pipe buffer and after that no write occurs. I'm not a Linux expert really. After `mkfifo something`, what command(s) should I use to handle this case? – james hofer Feb 24 '23 at 16:13
  • `tail -F namedpipe` might be of interest to you in this case. See for example https://askubuntu.com/a/1416100 – Raffa Feb 24 '23 at 17:03
  • @Raffa I tried to use `tail -F my_pipe` after creating the pipe and it's working! But one question, does it consume high memory or any resource in scale? – james hofer Feb 24 '23 at 17:54
  • You can, if you want, redirect its output to e.g. `/dev/null` and it should use very minimal resources AFAIK. – Raffa Feb 24 '23 at 18:30
  • 1
    @Raffa Could you post your comments as an answer? – james hofer Feb 24 '23 at 18:51

2 Answers2

1
tail -F namedpipe

might be of interest to you in this case … It should keep the pipe open and writable for your service constantly.

You can as well, if you want, discard its output to minimize used resources by redirecting it to e.g. /dev/null like so:

tail -F namedpipe > /dev/null
Raffa
  • 24,905
  • 3
  • 35
  • 79
0

Use socat to extract the data from the socket as soon as possible, put it into a file for example, and then read from it in another socat to another socket:

$ socat gopen:socket1 create:file1 
$ mkfifo socket2
$ socat gopen:file1 gopen:socket2

You might play with multiple socat options.