Skip to content

Instantly share code, notes, and snippets.

@luizfilipe
Created January 25, 2021 12:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luizfilipe/a7e7a61dccefd1745b22dd294373b52b to your computer and use it in GitHub Desktop.
Save luizfilipe/a7e7a61dccefd1745b22dd294373b52b to your computer and use it in GitHub Desktop.
This script kills applications that are running on a given port
#!/usr/bin/env node
const { exec } = require('child_process')
const findNumber = arg => !isNaN(parseInt(arg, 10))
const port = process.argv.find(findNumber)
const execCommand = command =>
new Promise((resolve, reject) =>
exec(command, (err, stdout, stderr) =>
err ? reject(err) : resolve({ stdout, stderr })
)
)
execCommand(`kill -9 "$(lsof -t -i:${port})"`)
.then(
() => (
console.log(`Application on port ${port} was killed`), process.exit(0)
)
)
.catch(
() => (
console.log(`There's no application running on port ${port}`),
process.exit(1)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment