Question

I'm trying to watch log rotated files with node.js and parse each line for keywords. The Problem is: after logrotate I'm still watching the old file because of the opened file discriptor.

I'm new to this and have two questions:

  1. Is it advisable to check after every line the inode and if it's not the same close the fd and read the file 'filename' again?
  2. What's the best way achieving this? Trying to avoid callbacks and use events instead or is there an library already?

Thanks!

Was it helpful?

Solution

When the log file is rotated, it is being renamed.

The fs.watch function in node.js has the ability to notify you when the file changes.

http://www.nodejs.org/api/fs.html#fs_fs_watch_filename_options_listener

Try something like:

fs.watch('filetowatch', function (event) {
  if (event === 'rename') {
    // close the file descriptor you are reading from and re-open.
  }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top