Question

A node.js script calls the maxima computer algebra system and redirects my input to the stdin of maxima. Maxima the writes the processed input to a temporary text file where there is a new line for every result maxima returns.

Can node watch for new data written to the file and somehow capture this data (only the new line which is written to the file, not the whole file)?

I already tried fs.watchFile but was unable to capture the actual data returned by it.

Thanks.

Was it helpful?

Solution

fs.watchFile plugs in to the actual file watching functionality in your Operating System (inotify in Linux), it does nothing more than just routing these events through. Any logic on which lines has been changed etc. has to be implemented by yourself. As it's a log file you probably are only interested in the tail, and you can reuse other programs for that. See this example for instance.

If you want to roll your own just do something like:

fs.watchFile("/path/to/log.txt", function (prev, curr) {
    // verify writes
    if (x && y && z) {
        fs.readFile("/path/to/log.txt", "utf8", function (err, body) {
             // check what has been appended
        });
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top