-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from kethinov/1.0.0
1.0.0
- Loading branch information
Showing
7 changed files
with
3,821 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# crossplatform-killport Changelog | ||
|
||
## Next version | ||
|
||
- Put your changes here... | ||
|
||
## 1.0.0 | ||
|
||
- Initial version. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
License | ||
=== | ||
|
||
All original code in this module is licensed under the [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/). Commercial and noncommercial use is permitted with attribution. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,35 @@ | ||
# crossplatform-killport | ||
💀 Kill the process running on a given port in any operating system. | ||
|
||
Command line program to kill the process running on a given port in any operating system written in Node.js. | ||
|
||
Unlike many of the other programs out there that do this, this one works in Windows, Linux, and Mac, it is very easily installed in any OS using `npm`, and it is actively maintained by a [team](https://github.com/orgs/rooseveltframework/people) of people rather than being just some guy's project from several years ago that may or may not work. | ||
|
||
## Usage | ||
|
||
Install to your system: | ||
|
||
To install to your system: | ||
|
||
``` | ||
npm i -g crossplatform-killport | ||
``` | ||
|
||
Kill whatever process is using port 8080: | ||
|
||
Use from within a Node.js project: | ||
|
||
``` | ||
node node_modules/.bin/crossplatform-killport/killport.js 8080 | ||
``` | ||
|
||
|
||
|
||
``` | ||
killport 8080 | ||
``` | ||
|
||
Or to use via `npx`: | ||
|
||
``` | ||
npx crossplatform-killport 8080 | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#!/usr/bin/env node | ||
|
||
const net = require('net') | ||
const { spawnSync } = require('child_process') | ||
|
||
// parse command-line arguments | ||
let silent = false | ||
for (const arg of process.argv) { | ||
if (arg === '--silent') { | ||
silent = true | ||
break | ||
} | ||
} | ||
|
||
const port = parseInt(process.argv[2], 10) | ||
if (isNaN(port)) { | ||
console.error('Please provide a valid port number.') | ||
process.exit(1) | ||
} | ||
|
||
// function to check if a port is in use | ||
function checkPort (port, callback) { | ||
const server = net.createServer() | ||
|
||
server.once('error', (err) => { | ||
if (err.code === 'EADDRINUSE') callback(null, true) // port is in use | ||
else callback(err) | ||
}) | ||
|
||
server.once('listening', () => { | ||
server.close() | ||
callback(null, false) // port is not in use | ||
}) | ||
|
||
server.listen(port) | ||
} | ||
|
||
// function to find the PID of the process running on the given port | ||
function findPidOnPort (port) { | ||
const platform = process.platform | ||
let command, args | ||
|
||
if (platform === 'win32') { | ||
// windows command to find the PID | ||
command = 'netstat' | ||
args = ['-ano'] | ||
} else { | ||
// *nix command to find the PID | ||
command = 'lsof' | ||
args = ['-i', `:${port}`] | ||
} | ||
|
||
const result = spawnSync(command, args, { encoding: 'utf8', shell: false }) | ||
|
||
if (result.error) { | ||
if (!silent) console.error(`Error finding process on port ${port}: `, result.error) | ||
return null | ||
} | ||
|
||
if (!result.stdout) { | ||
if (!silent) console.log(`No process found running on port ${port}`) | ||
return null | ||
} | ||
|
||
let pid | ||
if (platform === 'win32') { | ||
// extract PID on windows | ||
const lines = result.stdout.trim().split('\n') | ||
for (const line of lines) { | ||
const parts = line.split(/\s+/) | ||
if (parts[2] === `0.0.0.0:${port}` && parts[4] === 'LISTENING') { | ||
pid = parseInt(parts[5]) | ||
break | ||
} | ||
} | ||
} else { | ||
// extract PID on *nix systems | ||
const lines = result.stdout.trim().split('\n') | ||
const parts = lines[lines.length - 1].trim().split(/\s+/) | ||
pid = parts[1] | ||
} | ||
|
||
return pid | ||
} | ||
|
||
// function to kill the process running on the given port | ||
function killProcessOnPort (port) { | ||
const pid = parseInt(findPidOnPort(port)) | ||
if (pid) { | ||
try { | ||
process.kill(pid, 'SIGTERM') | ||
if (!silent) console.log(`Killed process ${pid} running on port ${port}`) | ||
} catch (err) { | ||
if (!silent) console.error(`Error killing process ${pid}: `, err) | ||
} | ||
} else { | ||
if (!silent) console.log(`No process found running on port ${port}`) | ||
} | ||
} | ||
|
||
// check if the port is in use and kill the process if it is | ||
checkPort(port, (err, isInUse) => { | ||
if (err && !silent) console.error('Error checking port: ', err) | ||
else if (isInUse) { | ||
if (!silent) console.log(`Port ${port} is in use. Attempting to kill the process...`) | ||
killProcessOnPort(port) | ||
} else if (!silent) console.log(`Port ${port} is available.`) | ||
}) |
Oops, something went wrong.