This question already has an answer here:

Is there a way to take a process substitution like <(grep foo some/file), and save it to a "file-like entity" on disk (reminiscent of a fifo), so that, every time this thing was opened for reading by some process P, the grep foo some/file command (or whatever) would get run anew, and the output returned as the content being read by the process P?

marked as duplicate by Gilles, Scott, cas, Anthon, Rui F Ribeiro May 15 '16 at 7:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    That's process substitution <(blah blah) and that's what it does... Try cat <(date) then after a few seconds run it again. – don_crissti May 14 '16 at 0:16
  • @don_crissti: I mean something that lives on the file system, like a named pipe. It gets listed by ls, etc. It has no content though; or rather, its content gets generated dynamically whenever a process attempts to read it. – kjo May 14 '16 at 0:23
  • @kjo: What should that content be? – Julie Pelletier May 14 '16 at 0:39
  • You could mkfifo and have a writer queued on it within a loop. Downsides are: 1, that the writer would have already started, so date for example would be the time the command started rather than the time its output was consumed. 2, you'd have processes active (although blocked) using static resources. Thinking some more, you might be able to do something with socat. – roaima May 14 '16 at 0:44
  • It sounds like you want a virtual filesystem along the lines of /proc or /sys but set up with customized contents. – Wildcard May 14 '16 at 0:49

This isn't possible with standard UNIX/POSIX facilities. Your imperfect options are:

  • Just use a shell script, and have the "opening" application (or your shell) execute it instead of opening it for reading. Some apps may support doing this, otherwise you'll have to resort to a shell script wrapper.
  • Use a fifo, and write a daemon that writes to it. This of course means you need to have the daemon started, and it won't work correctly if the fifo is opened multiple times simultaneously.
  • Use a virtual filesystem, e.g. FUSE on Linux, to present a virtual file with the semantics that you want. You still need a daemon that implements the virtual filesystem for this.
  • +1. Another option is to write a simple network service that outputs the required data (and run it as a standalone daemon or from inetd or xinetd or similar). client would make a tcp connection to the address (e.g. localhost) and port to get the data. If you write it as a standalone, you have a lot more flexibility but you'll have to write your own code to handle multiple simultaneous connections (or use the appropriate library for your language of choice). – cas May 14 '16 at 4:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.