Say I start a node.js process like so:

node script.js &

is there a way to pipe data into that process after it has started?

normally, we do this:

cat <file> | node script.js 

but the problem I am seeing is that sometimes the node.js process won't actually start up beforehand.

My goal is to ensure that the node.js process starts up no matter what, even if there is no data in the file.

So I was thinking of starting node.js process as background process, then connecting it to a pipe, not sure if that's possible.

1 Answer 1


It sounds like you're looking for some variant of named pipes (fifos) created with mkfifo.

Then you can do something like this:

mkfifo my-pipe
node script.js < my-pipe &

Note that will block until something opens my-pipe for writing. So if you want script.js to be able to open the file (but not of course read anything from it yet), then you need to open it for writing; a trivial way is just to redirect a sleep to it (sleep 3650d > my-pipe &). This will also stop script.js from getting an EOF (end of file) until you kill the sleep (when nothing has a fifo open for write and all the data is gone, the reading side gets an EOF).

Now, you can feed it more lines whenever by writing to my-pipe. Any write that doesn't seek (or mmap, etc.) will work. You could test with echo 'DATA' > my-pipe.

So, taken together:

mkfifo my-pipe
node script.js < my-pipe &
sleep 3650d > my-pipe &
get-data-command > my-pipe     # placeholder for real command to get data
get-data-command > my-pipe
# and so on, for more data.
kill $sleep_pid                # kill the sleep, giving script.js an EOF.

As Wildcard points out, you may actually want script.js to open a listening socket and accept data that way. You can use Unix domain sockets if you want to avoid having to deal with authentication as you'd have to on a TCP socket (because Unix sockets use file permissions instead).

  • Interesting use of sleep, +1. It seems that's a bit hacky, though. Could it be that the Original Poster is actually trying to reinvent listening sockets?
    – Wildcard
    Commented Jul 8, 2017 at 4:57
  • I tried this echo "dummy" | cat my.fifo | node script.js ....... it still blocks, the node.js script will not start, I do not get it Commented Jul 8, 2017 at 5:26
  • my goal is to hook up the pipe, but to get the node.js script to start running, no matter what Commented Jul 8, 2017 at 5:27
  • 1
    @AlexanderMills does that full example help? It shows how to start up the script, the sleep, and then how to feed data through the fifo. Finally, it shows how to kill the sleep, so it gets an EOF on the fifo.
    – derobert
    Commented Jul 8, 2017 at 5:46
  • 1
    @KamilMaciorowski depends on where its running from. If that's from a system startup script, cron @reboot, etc., then not needed. From an interactive login shell, might be—though disown would probably be sufficient. (Though at least my bash has huponexit unset, and I think that's the default).
    – derobert
    Commented Jul 8, 2017 at 6:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .