Ziva
Ziva

Reputation: 3511

Close many ports at once

When I want to kill a process on a given port (let say 8000) I'm using command:

kill -9 `lsof -i :8000`

Is there any way to close many ports at once? I have to kill process on ports 8000-9000 and doing this by hand is pretty inefficient.

Upvotes: 1

Views: 201

Answers (1)

mattias
mattias

Reputation: 2108

Ok, first attempt to give you a working script for this.

#!/bin/bash
for i in {8000..9000}
do
   kill -kill `lsof -t -i tcp:$i`
done

Put the above in a .sh file, myportrangekiller.shand make it executable, chmod +x myportrangekiller.sh and run it with $ ./myportrangekiller.sh from your terminal. This will loop through ports 8000-9000 and kill those processes.

Hard for me to verify if it works, as I am getting the following when running your command in OS X El Capitan,

kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

Btw, I changed your command slightly, from lostto lost, kind of assumed that you misspelled that binary's name.

Upvotes: 1

Related Questions