Skip to content

Instantly share code, notes, and snippets.

@dstibrany
Last active December 26, 2015 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dstibrany/7094835 to your computer and use it in GitHub Desktop.
Save dstibrany/7094835 to your computer and use it in GitHub Desktop.
var cluster = require('cluster');
var http = require('http');
var cp = require('child_process');
if (cluster.isMaster) {
var worker = cluster.fork();
}
else {
server = http.createServer(); // number of servers is arbitrary
server2 = http.createServer();
server.listen(10011, 'localhost')
server2.listen(10012, 'localhost')
setTimeout(run, 100); // need this timeout to recreate
}
function run() {
var sleep = cp.spawn('sleep', ['7']);
var lsof = cp.spawn('lsof', ['-p', sleep.pid]);
var grep = cp.spawn('grep', ['LISTEN']);
lsof.stdout.on('data', function(data) {
grep.stdin.write(data);
});
lsof.on('close', function() {
grep.stdin.end();
});
grep.stdout.on('data', function(data) {
console.log(data.toString());
});
}
@dstibrany
Copy link
Author

output is (roughly)

sleep 46028 dstibrany 11u IPv4 0x28b8ad9c32befc2d 0t0 TCP localhost:10011 (LISTEN)
sleep 46028 dstibrany 13u IPv4 0x28b8ad9c350db37d 0t0 TCP localhost:10012 (LISTEN)

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