Way to implement custom files that work like the ‘files’ in the /proc file system

fifo

I'm looking for something like a persistent named pipe… something that I can cat or grep multiple times, and always get the current state of whatever process is feeding into the pipe.

For example, let's say that I create a named pipe called /tmp/timestamp, and then use date to write to it:

mkfifo /tmp/timestamp
date --iso-8601=seconds > /tmp/timestamp

At this point, the call to date will block, waiting for /tmp/timestamp to be read …

cat `/tmp/timestamp`

Will un-block date, I'll see something like 2017-03-18T16:11:54-04:00 written to stdout, and date will terminate.

… but what if I want an updated date every time I cat /tmp/timestamp?

I guess that

while :; date --iso-8601=seconds > /tmp/timestamp; done

will work, but I'd like to know if a) there are any non-obvious issues with this approach and b) if there's a way to do it which doesn't require a loop.

I would also like to set this up so that it launches automatically, making the fifo always available.

In terms of why I want this to be in pipes — the information in question is stored in a database backing a web application. Most of our tech support folks are entirely comfortable logging into the servers via ssh and running queries against the database, but there are certain vital statistics that would be really handy to simply grep from files. Being able to ls the directory containing the named pipes would make all of this discoverable… essentially, I'm not doing this because I have to, I'm doing it because it's a metaphor that I think will work well.

Best Answer

You could implement a custom filesystem either as a kernel module or by using fuse. Within that custom filesystem you can then have whatever virtual files you want.