Skip to content

Instantly share code, notes, and snippets.

@lancejpollard
Forked from bratish/tail-f.js
Created June 11, 2012 20:28
Show Gist options
  • Save lancejpollard/2912470 to your computer and use it in GitHub Desktop.
Save lancejpollard/2912470 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