Created
January 25, 2021 12:56
-
-
Save luizfilipe/a7e7a61dccefd1745b22dd294373b52b to your computer and use it in GitHub Desktop.
This script kills applications that are running on a given port
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
#!/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