I am trying to create a bash function which should kill all processes using some ports specified in port_array. The kill-port function works if I call it myself with a port e.g. with kill-port "80";. But if I call it inside the for loop I get some strange error from kill:
Usage:
kill [options] <pid> [...]
Options:
<pid> [...] send signal to every <pid> listed
-<signal>, -s, --signal <signal>
specify the <signal> to be sent
-l, --list=[<signal>] list all signal names, or convert one to a name
-L, --table list all signal names in a nice table
-h, --help display this help and exit
-V, --version output version information and exit
For more details see kill(1).
Here is the code:
#!/bin/bash
port_array=( 5057 5061 5056 );
function kill-ports () {
ports=( "$@" )
for i in "${ports[@]}";
do
kill-port $i;
done
};
function kill-port () {
lsof -i tcp:"$1" | awk 'NR!=1 {print $2}' | xargs kill;
}
kill-ports "${port_array[@]}";
I am probably overseeing my mistake, but I would be thankful if someone could tell me what the problem here is.
Regards,
Silas