Home > database >  Unable to get the correct syntax for Kill Command Linux inside a ZSH function
Unable to get the correct syntax for Kill Command Linux inside a ZSH function

Time:07-07

I am really confused as to what is happening

I copied from one of SO answers a simple function (it's inside my .zshrc)

function killport() {
    sudo kill -s STOP $(fuser -n tcp $1 2> /dev/null)
}

But when I run it I get

$ killport 80  

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).

I also get the same if I just try to use sudo kill -9 `sudo lsof -t -i:8080` .

The above mentioned sudo kill -9 worked for me just yesterday, but today I haven't been able to get it working. Tried adding the -s flag for a signal but it should default to STOP

CodePudding user response:

As requested, turning my comment into an answer:

First calculate the pid to be killed, then test whether the calculation looks reasonable, and then kill the process.

pid=$(fuser -n tcp "${1:?parameter missing}" 2>/dev/null)
if [[ -z $pid || $pid == *[^[:digit:]]* ]]
then
  echo "Error: Can not find PID for '$1'" 1>&2
else
  sudo kill -s STOP $pid
fi
  • Related