Coder Social home page Coder Social logo

kill-port's Introduction

Logo

kill-port

Kill process running on given port


Table of Contents

Install

With npm:

npm install --save kill-port

With yarn:

yarn add kill-port

With pnpm:

pnpm add kill-port

Usage

const kill = require('kill-port')
const http = require('http')
const port = 8080

const server = http.createServer((req, res) => {
  res.writeHead(200, {
    'Content-Type': 'text/plain'
  })

  res.end('Hi!')
})

server.listen(port, () => {
  setTimeout(() => {
    
    // Currently you can kill ports running on TCP or UDP protocols
    kill(port, 'tcp')
      .then(console.log)
      .catch(console.log)
  }, 1000)
})

API

The module exports a single function that takes a port number as argument. It returns a promise.

CLI

You can use kill-port as a global package.

Install the package globally:

$ npm install --global kill-port
# OR
$ yarn global add kill-port

Then:

$ kill-port --port 8080
# OR
$ kill-port 9000
# OR you can use UDP
$ kill-port 9000 --method udp

You can also kill multiple ports:

$ kill-port --port 8080,5000,3000
# OR
$ kill-port 9000 3000 5000

You can also use npx to kill-port without installing:

# Kill a single port
$ npx kill-port --port 8080
$ npx kill-port 8080
# Use UDP
$ npx kill-port 9000 --method udp
# Kill multiple ports
$ npx kill-port --port 8080,5000,3000
$ npx kill-port 9000 3000 5000

Contributing

Got an idea for a new feature? Found a bug? Contributions are welcome! Please open up an issue or make a pull request.

License

MIT © Tiaan du Plessis

kill-port's People

Contributors

baumatron avatar dependabot[bot] avatar fergusean avatar garygeorgeu avatar greenkeeper[bot] avatar kidandcat avatar lfsousa avatar nvh95 avatar oxxxra avatar renovate-bot avatar tiaanduplessis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

kill-port's Issues

Sh is not a function

Hi,

I using Mac switching from bash to zsh, then zsh to bash.

TypeError, sh is not a function
Kill-port/index.is:18:10

Add option for kill signal

I need to send SIGINT and not SIGKILL, SIGTERM would be good too. On windows I am not sure if there is antyhing like this.

PID cannot be terminated unless forcefully with /F option

Trying to kill an ExpressJS server instance (in JS, not CLI) because my Hot Reloading app causes it to duplicate listen messages, and am getting this error on kill command:

cmd: TaskKill /PID 28236",
code: 1,
stderr: "ERROR: The process with PID 28236 could not be terminated.
Reason: This process can only be terminated forcefully (with /F option).",
stdout:""

Don't see any means to add flags to the kill method. Is there a solution to this?

--port 9229 is causing a crash

root@7a607b52938a:/app# kill-port --port 9229
/usr/local/lib/node_modules/kill-port/cli.js:9
let port = args.port ? args.port.split(',') : args.unknown
^

TypeError: args.port.split is not a function
at Object. (/usr/local/lib/node_modules/kill-port/cli.js:9:34)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3

"Could not kill process on port" in Alpine based docker images (fix suggestion)

Using this package in order to solve a problem with Node.js --inspect in the official alpine docker image, I receive that error message.

I solved this problem using the following command:
netstat -lnpt | awk '$4 ~ /:PORT$/ {sub(/\/.*/, "", $7); print $7}'
but i don't know if can be used in other circumstances.

EDIT: another solution compatible with npm/yarn scripts is:
netstat -lnpt | awk '$4 ~ /:PORT$/ {print $7}' | cut -d'/' -f1
so can be used like:

{
  "scripts": {
    "dev:inspect:port": "netstat -lnpt | awk '$4 ~ /:PORT$/' | awk '{print $7}' | cut -d'/' -f1"
}

Use `fuser` on Linux

Hi,

You can use fuser to identify and kill processes using port.

  return sh(`fuser --kill --namespace ${method} ${port}`)

It's installed by default on Ubuntu and OSMC (Linux distribution based on Debian). lsof isn't installed by default on OSMC (maybe also on Debian).

Breaking change?

Hi, @tiaanduplessis. The major version update to version 2.0 implies that a breaking change was introduced but I can't find any release notes or change log. Could you provide some info on what the breaking change was?

Quite slow on Windows

While it works, it's quite slow on Windows for me. I was tinkering with the source code a bit and found that changing netstat -ao into netstat -aon makes it significantly faster. On my laptop for example, it's about 10 seconds faster.

I know basically nothing about netstat though, so there may be a really good reason why -n isn't used... but just in case it's an oversight, here's the heads-up :-)

Additional maintainers?

Hi! This is an amazing library and a very useful one, but it seems like its development has quiet down a bit. We would be happy to help with maintaining it in our organization - would you be open to accepting additional maintainers?

Not working on windows

The CLI is not working on Windows. It doesn't recognize the kill-port command after installing globally either in bash or powershell. Why is this happening?

Feature idea/request: Option to kill the ancestor's process tree

Killing a dotnet watch or nodemon process tree that hosts a listen port is never fun, but kill-port could actually make it fun!

My thought is kill-port could have an option to find the parent process of the process that is listening on the port and then kill the parent process' tree. Also, I can imagine, given how intermediate launch shell or node processes could be between the daemon watcher and the listening process, there should be a setting to indicate how many levels of ancestors to traverse up before doing the tree kill with the default being 1. So, something like:

kill-port --port 5000 --parent     # kills the parent process tree of the process listening on port 5000
kill-port --port 3000 --ancestor 3 # kills the great-grandparent process tree of the process listening on port 3000 

Github version not deployed.

I noticed the 1.6.0 release does not contain the /F flag for windows TaskKill. I need /F in order for kill-port to kill mysqld.

Can you deploy this 1.7.0, please?

It takes too long to kill with version 2.0

I rolled back to version 1 because it takes too much time to kill a process compared to version 1
It also throws an error if port is not found.

I think it's better to keep the existing logic to kill a port
lsof -i ${method === 'udp' ? 'udp' : 'tcp'}:${port} instead of using lsof -i -P, which takes too much time

IMO kill() must resolve when

  • when a port is found and kill
  • when a port is not found and nothing to kill

Unable to kill process running on port in Ubuntu 18.04 docker container

As part of an npm script I am stating a wiremock server using the wiremock npm package. Then executing some cypress tests and once they are complete am attempting to run: kill-port <PORT> on the port I am starting wiremock on.

When running locally (macOS Catalina 10.15.5), works just fine, but in an Ubuntu 18.04 docker container it does not kill the wiremock process

Could not find declaration file for module "shell-exec" (Typescript)

Environment:

image

Reproducing:

Setup

Initialize new node repository
npm i kill-port
npm i --save-dev @types/kill-port

index.ts :

import killPort from "kill-port";

tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitAny": true,
        "removeComments": true,
        "preserveConstEnums": true,
        "esModuleInterop": true,
        "sourceMap": true,
        "lib": [
            "ES2015"
        ],
        "moduleResolution": "node",
        "target": "ES6",
    },
    "files": [
        "index.ts"
    ]
}

Run: tsc

Output

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.