Skip to content

Instantly share code, notes, and snippets.

@EricLondon
Created July 9, 2015 12:51
Show Gist options
  • Save EricLondon/f5e4722c15d1e9d6a7d1 to your computer and use it in GitHub Desktop.
Save EricLondon/f5e4722c15d1e9d6a7d1 to your computer and use it in GitHub Desktop.
nodejs spawn shell process (ps aux) and filter
var run_cmd = function(cmd, args, callBack) {
var spawn = require('child_process').spawn;
var child = spawn(cmd, args);
var resp = "";
child.stdout.on('data', function (buffer) { resp += buffer.toString() });
child.stdout.on('end', function() { callBack (resp) });
}
var check_ps_aux = function(service) {
run_cmd( "ps", ["aux"], function(output) {
var output_lines = output.split(/\r?\n/);
var filtered = output_lines.filter(function(x){
var regex = new RegExp(service.ps_search, 'i');
return regex.test(x);
});
// update data
service.is_running = filtered.length > 0 ? true : false;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment