2

I'm looking to supply a process that only reads from a local file with content from an http URL. The process is a daemon that is hard-coded to read a specific file in it's current working directory and reads it whenever a particular event happens.

I have no control over the event that triggers the file read and I want to supply near real time data instead of constantly dumping data via a curl script to the file.

What I thought might work would be a fifo that executes a command whenever it is read. This would then be connected to netcat or curl to retrieve the contents of the URL (xml data). Is this possible?

Paul Allen
  • 123
  • 5
  • If the process is well-behaved, that is, it opens the file, reads it sequentially, and closes it, a named pipe ought to work. Something like `while true; do (echo -n ""; curl ... > tmp; cat tmp) > /path/to/fifo; done`. – Mark Plotnick Jan 21 '15 at 23:48
  • See also [Create a virtual file that is actually a command](http://unix.stackexchange.com/questions/66990/create-a-virtual-file-that-is-actually-a-command). I'm not voting to close as a duplicate because the specific use case of reading from an HTTP URL can have simpler answers than the generic idea of executing commands to implement file reading and writing. – Gilles 'SO- stop being evil' Mar 30 '15 at 12:03

1 Answers1

5

It's impossible to make reading from a pipe launch a process on a normal filesystem. But there are other methods that should do what you need.

You can run the content-generating program in a loop. Each time the FIFO is opened for writing, this will block until a reader comes along.

while true; do
  generate-content >fifo
done

Another approach is to make a FUSE filesystem that generates whatever content you like — the data returned to the reader is generated by the filesystem driver, which is an ordinary program, so you can make it do whatever you like. Building a FUSE filesystem driver is a lot of work though, so unless you find one that already does exactly what you need this is probably overkill. Some possible filesystems that may help you are:

  • ScriptFS — judging by the description, this does exactly what you need: “replicates a local file system, but replaces all files detected as ‘scripts’ by the result of their execution”. I've never used it.
  • HTTPFS — reading from the file triggers a web download; connect it to localhost and run a small webserver such as lighttpd or thttpd that supports CGI or a similar method to serve dynamic content. If the data you need is already downloaded from a webserver, simply point HTTPFS at it.
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • It might not be *(easily)* possible to react to a fifo read *(though flooding a pipe could work, maybe)* but it would be possible with a serial line/terminal. `screen` could be setup to handle this, or SLIP and/or PPP *(even SYNC_PPP)* could be used - this is kind of what they're for, as I think. – mikeserv Jan 22 '15 at 02:32