2

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

0
2
kill-ports() {
   for port in "$@"; do fuser -n tcp "$port" -k -TERM; done
}

(You absolutely do not need the quotes in this case, but it's always good to use them, or else you'll get complains/edits from people.)

2

There is a solution, thanks to @StéphaneChazelas comment:

"Just do:

lsof -ti "tcp:$1" | xargs -r kill,

that's what -t is for (and -r tells xargs not to run the command if there's no argument. That's for GNU xargs. Some other implementations like FreeBSD do that automatically)"

In the end it looks like this and also works (Some cleanup by me too):

#!/bin/bash

port_array=( 5057 5061 5056 );
kill-ports() {
   for port in "$@"; 
   do
       kill-port "$port";
   done
};

kill-port () {
   lsof -ti "tcp:$1" | xargs -r kill;
}

kill-ports ${port_array[@]};

EDIT:

@PSkocik's solution was posted after @StéphaneChazelas comment, but works fine too:

#!/bin/bash

port_array=( 5057 5061 5056 );

kill-ports() {
   for port in "$@"; do fuser -n tcp "$port" -k -TERM; done
}

kill-ports ${port_array[@]};
-3

Try appending -9 to kill

lsof -i tcp:"$1" | awk 'NR!=1 {print $2}' | xargs kill -9;
5
  • That won't make any difference, it will just kill those programs it finds in a less elegant way. The issue here is that there are sometimes no programs listening on the port in question. – terdon May 20 '15 at 12:18
  • 1
    Not a good suggestion – iruvar May 20 '15 at 12:18
  • Error checking is author's resposibility - He needs to take care of those. – alpa May 20 '15 at 12:20
  • This doesn't work, same error. But Stéphane Chazelas solution works, I am just waiting for his answer on whether he or me should post the solution – lenovouser May 20 '15 at 12:22
  • Comments are not for extended discussion; this conversation has been moved to chat. – terdon May 20 '15 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.