Skip to content

Instantly share code, notes, and snippets.

@sebassdc
Created January 28, 2017 08:00
Show Gist options
  • Save sebassdc/56310ed5472d659065464e65a56a5ceb to your computer and use it in GitHub Desktop.
Save sebassdc/56310ed5472d659065464e65a56a5ceb to your computer and use it in GitHub Desktop.
A little script that kills the process that use a given port
#! /home/sebassdc/.nvm/versions/node/v6.9.1/bin/node
// place up there the output of "whereis node"
const cp = require('child_process')
if (process.argv.length < 3 || process.argv.length > 3) {
throw new Error('must be one argument')
}
cp.exec(`netstat -ap | grep :${process.argv[2]}`, (err, stdout, stderr) => {
if (err) {
console.log(`maybe the port: ${process.argv[2]} is not been used`)
process.exit()
}
let stdout_splited = stdout.split(' ')
let unwanted_dude;
stdout_splited.forEach(dude => {
if (dude.match(/\b\/\b/g)) {
unwanted_dude = dude
}
})
console.log(`${unwanted_dude} was using the port`)
cp.exec( `kill -9 ${unwanted_dude.split('/')[0]}` , (err, stdout, stderr) => console.log(stdout))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment