Skip to content

Instantly share code, notes, and snippets.

@justinrainbow
Created May 16, 2011 19:58
Show Gist options
  • Save justinrainbow/975213 to your computer and use it in GitHub Desktop.
Save justinrainbow/975213 to your computer and use it in GitHub Desktop.
Tail a remote file on multiple servers with Node.js
var sys = require('sys'),
spawn = require('child_process').spawn,
// args from command line
filename, servers;
if (process.ARGV.length < 4) {
return sys.puts("Usage: node remote-tail.js filename server1 [serverN]");
}
filename = process.ARGV[2];
servers = process.ARGV.slice(3);
function writeData(host, data) {
console.log(host + ": " + data);
}
function readData(host, data) {
var lines = data.toString().split("\n")
for (var i = 0, len = lines.length; i < len; i++) {
if (lines[i].length > 0) {
writeData(host, lines[i])
}
}
}
for (var x = 0, len = servers.length; x < len; x++) {
var server = servers[x];
// Look at http://nodejs.org/api.html#_child_processes for detail.
var tail = spawn("ssh", [server, "tail", "-f", filename]);
tail.stdout.on("data", function(data) {
readData(server, data);
});
}
@victorjonsson
Copy link

For anyone stumbling in here, you might want to check in https://www.npmjs.com/package/remote-tail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment