Created
July 9, 2015 12:51
-
-
Save EricLondon/f5e4722c15d1e9d6a7d1 to your computer and use it in GitHub Desktop.
nodejs spawn shell process (ps aux) and filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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