-1

part one:

I have a text.txt file in linux ubuntu.

I want to watch the file with tail for grabbing the last content of the file.

# command linux

tail -f text.txt

this command returns all of the content.

so I changed it to :

# command linux

tail -f -n 1 text.txt

but the same result again.

I examine the line of file with :

# command linux
wc -l text.txt

when content is appended to file, the line number counts changes.

so first of all what's wrong?

second, question is about nodejs

nodejs:

I want to run child_process(spawn):

let tailing_my_file = spawn("tail -f ./text.txt")    // in root directory of project

is there any better idea for watching file content? and off the record, the file is getting huge. is it wise to use spawn for such heavy loads?

Babak Abadkheir
  • 2,222
  • 1
  • 19
  • 46
  • The file size should not incur a significant performance overhead when reading a file. It is smart enough to read blocks backwards from the end, count the lines, then print forwards. Even if it goes back several blocks, they will still be in cache. (BusyBox may have an inferior version.) – Paul_Pedant Jul 08 '20 at 12:24

1 Answers1

1

You could just fs.read the changes using the file descriptor:

const fs = require('fs')

const len = 26 // how many bytes you want to read
const offset = 0 // start from the beginning of the file at start
const pos = null // this parameter does the trick: the file descriptor is updated to the last byte read

fs.open('text.txt', 'r', (err, fd) => {
  if (err) {
    console.log(err)
    process.exit(1)
  }

  // fd is a file descriptor
  const kill = setInterval(keepReading, 1000)

  function keepReading () {
    fs.read(fd, Buffer.alloc(len), offset, len, pos, (err, bytes, buff) => {
      if (err) {
        console.log(err)
        process.exit(1)
      }

      if (bytes !== 0) {
        console.log(buff.toString())
      }
    })
  }

  process.once('SIGINT', () => {
    clearInterval(kill)
  })
})

The pro of this solution is that is cross platform.

is it wise to use spawn for such heavy loads?

Spawn it is slower than reading a file descriptor.

PS. in node 13 there is the more friendly interface with options

Manuel Spigolon
  • 11,003
  • 5
  • 50
  • 73