1

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?

Michael Homer
  • 76,565
kjo
  • 15,339
  • 25
  • 73
  • 114
  • 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 00: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 00:23
  • @kjo: What should that content be? – Julie Pelletier May 14 '16 at 00: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. – Chris Davies May 14 '16 at 00: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 00:49
  • You probably need to specify an OS too (or at least, the answer for POSIX is "no"). – Michael Homer May 14 '16 at 00:55

1 Answers1

3

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.
marcan
  • 958
  • +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 04:08