Skip to content

Instantly share code, notes, and snippets.

@MonkeyIsNull
Created January 20, 2012 03:43
Show Gist options
  • Save MonkeyIsNull/1644842 to your computer and use it in GitHub Desktop.
Save MonkeyIsNull/1644842 to your computer and use it in GitHub Desktop.
An rsync watcher
var fs = require('fs'),
dir = "/tmp/rpki/repository",
tempFile = "/tmp/replace.rsync.conf",
relFile = "/etc/rsyncd.conf",
repo = "[repository]\n\tpath = /tmp/rpki/repository/";
var util = require('util'),
spawn = require('child_process').spawn;
function kick_rsync() {
var rsync = spawn('rsync', ['--daemon']);
console.log("Done spawning rsync");
};
fs.watch(dir, function(e, filename) {
console.log("caught a " + e + " on " + filename);
fs.stat(dir + "/" + filename, function(err, stats) {
if(stats) {
if(stats.isDirectory()) {
console.log("got the change on: " + filename);
fs.writeFileSync(relFile, repo + filename + "\n");
var ps = spawn('ps', ['-C', 'rsync', '-o', 'pid=']);
ps.stdout.on('data', function(pid) {
console.log("killing proc: " + pid);
process.kill(pid, 'SIGHUP');
});
ps.on('exit', function(code) {
if(code !== 0) {
console.log("Rsync not running...");
kick_rsync();
} else {
// we killed it, give kernel time to reset
setTimeout(kick_rsync, 5000);
}
});
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment