Skip to content

Instantly share code, notes, and snippets.

@bratish
Created October 6, 2010 04:54
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bratish/612849 to your computer and use it in GitHub Desktop.
Save bratish/612849 to your computer and use it in GitHub Desktop.
/**
* A `tail -f` implementation in Node.js.
*
* Bratish Goswami
* bratishgoswami AT gmail DOT com
*
*/
var sys = require("sys"),
fs = require('fs'),
f = "/path/to/file",
startByte = 0;
fs.stat(f, function(err, stats){
if (err) throw err;
startByte = stats.size;
sys.puts(startByte);
});
fs.watchFile(f, function (curr, prev) {
fs.stat(f, function(err, stats){
if (err) throw err;
fs.createReadStream(f, {
start: startByte,
end: stats.size
}).addListener("data", function(lines) {
sys.puts(lines);
startByte = stats.size;
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment