- ps aux | grep node
- sudo kill -9 PID
Last active
November 15, 2017 14:59
-
-
Save DaniilBabanin/67b7c3635d563eecdeab839c63c26389 to your computer and use it in GitHub Desktop.
killnode
This file contains 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
/** | |
* Kill Processes containing searchPram string | |
* usage: node killer.js {{searchString}} | |
*/ | |
var exec = require('child_process') | |
.exec; | |
var searchParam = 'test/'; | |
var rootPassword = ''; // replace this with your root password | |
// look for search param argument passed by command promt | |
process.argv.forEach(function (val, index, array) { | |
console.log(index + ': ' + val); | |
if (index === 2) { | |
searchParam = val; | |
} | |
}); | |
// grep for processes containing node | |
dir = exec("ps aux | grep node", function (err, stdout, stderr) { | |
if (err) { | |
console.log('err: ', err); | |
} | |
var a = stdout.split("\n"); | |
var PIDS = a.map(function (elem) { | |
// find process containing searchParam | |
if (elem.indexOf(searchParam) !== -1) { | |
return elem; | |
} else { | |
return false; | |
} | |
}) | |
.filter(function (elem) { | |
return elem; | |
}) | |
.map(function (elem) { | |
params = elem.split(' '); | |
// get PIDS | |
var relevantPid = params.map(function (subElem) { | |
if (subElem.length > 2) { | |
return subElem.trim(); | |
} else { | |
return false; | |
} | |
}) | |
.filter(function (arg) { | |
return arg; | |
}); | |
return relevantPid[1]; | |
}); | |
// kill all found pids | |
var killed = PIDS.map(function (elem) { | |
killDir = exec("echo " + rootPassword + " | sudo -S kill -9 " + elem, function (err, stdout, stderr) { | |
if (err) { | |
console.log('err: ', err); | |
} | |
return elem; | |
}); | |
}); | |
}); | |
dir.on('exit', function (code) { | |
console.log('code: ', code); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment