Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a signal selection option #70

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ const server = http.createServer((req, res) => {

server.listen(port, () => {
setTimeout(() => {

// Currently you can kill ports running on TCP or UDP protocols
kill(port, 'tcp')
.then(console.log)
Expand Down Expand Up @@ -108,6 +107,8 @@ $ kill-port --port 8080
$ kill-port 9000
# OR you can use UDP
$ kill-port 9000 --method udp
# OR you can use SIGTERM
$ kill-port --signal SIGTERM 9000
```

You can also kill multiple ports:
Expand Down
8 changes: 5 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ const kill = require('./')
const args = require('get-them-args')(process.argv.slice(2))

const verbose = args.verbose || false
let port = args.port ? args.port.toString().split(',') : args.unknown
const signal = args.signal || 'SIGKILL'
const method = args.method || 'tcp'

let port = args.port ? args.port.toString().split(',') : args.unknown

if (!Array.isArray(port)) {
port = [port]
}

Promise.all(port.map(current => {
return kill(current, method)
return kill(current, method, signal)
.then((result) => {
console.log(`Process on port ${current} killed`)
console.log(`Process on ${method} port ${current} killed using signal ${signal}`)
verbose && console.log(result)
})
.catch((error) => {
Expand Down
22 changes: 20 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

const sh = require('shell-exec')

module.exports = function (port, method = 'tcp') {
module.exports = function (port, method = 'tcp', signal = 'SIGKILL') {
port = Number.parseInt(port)
signal = mapSignalToNumber(signal)

if (!port) {
return Promise.reject(new Error('Invalid port number provided'))
}

if (!signal) {
return Promise.reject(new Error('Invalid signal name provided'))
}

if (process.platform === 'win32') {
return sh('netstat -nao')
.then(res => {
Expand Down Expand Up @@ -40,7 +45,20 @@ module.exports = function (port, method = 'tcp') {
if (!existProccess) return Promise.reject(new Error('No process running on port'))

return sh(
`lsof -i ${method === 'udp' ? 'udp' : 'tcp'}:${port} | grep ${method === 'udp' ? 'UDP' : 'LISTEN'} | awk '{print $2}' | xargs kill -9`
`lsof -i ${method === 'udp' ? 'udp' : 'tcp'}:${port} | grep ${method === 'udp' ? 'UDP' : 'LISTEN'} | awk '{print $2}' | xargs kill -${signal}`
)
})
}

function mapSignalToNumber(signal) {
const signals = {
SIGHUP: 1,
SIGINT: 2,
SIGQUIT: 3,
SIGABRT: 6,
SIGKILL: 9,
SIGTERM: 15
}

return signals[signal]
}
6 changes: 5 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ describe('kill-port', () => {
})

test('should throw if no port is provided', () => {
expect(kill()).rejects.toThrow()
expect(kill()).rejects.toThrow('Invalid port number provided')
})

test('should throw if invalid signal is provided', () => {
expect(kill(9999, 'tcp', 'BADSIG')).rejects.toThrow('Invalid signal name provided')
})

test('should throw if no process running on given port', () => {
Expand Down