If I want to stop a program given its tcp port number or its file or named executable, can I run something like kill -tcpPortNumber $1 where the parameter $1 is a tcp port number or kill -filename $1 where the parameter is a filename? The background is a problem in the following question:


The problem does occur in practice and I think that user Bepetersn has a solution that looks like what I mean. sudo kill $(fuser -n tcp <port_number> 2> /dev/null)

But what I'd like to achieve is that the kill program could take tcp port number and/or filename as a parameter system-wide for any user, including sudo. So is it possible to add parameters system-wide to a system program like kill so that the tcp port number and/or a fileename can be parameters and that this change is permanent for any user including ubuntu sudo?

  • 2
    What about adding a kill -colour blue to kill the processes that output something in blue? What about if you want to renice those processes or trace those processes, should we add those options to every command that deal with processes? – Stéphane Chazelas Feb 20 '14 at 23:24
  • @StephaneChazelas But I'd rather have something easier to remember than sudo fuser -KILL -k -n tcp 8088. I have limited knowledge of why root can't use aliases and can't use functions and the most user-friendly that we can do is sudo fuser -KILL -k -n tcp 8088 to shutdown a server that has OutOfMemoryError on 8088. On Ubuntu you must be sudo to use port numbers lower than 1024, perhaps thas condition can be relaxed, but I had to be sudo to perform the command. – Niklas Rosencrantz Feb 21 '14 at 18:15

No it is not possible to modify tools such as kill via configurations in a manner that you're asking for. But given all tools are generally available in source form you can always take any tool and modify it it in any way you like.

Most tools are however extended in 1 of the following ways:

1. wrapping

Additionally most people tend to "wrap" the commands that are typically provided on most Unix systems using either shell scripts or aliases or functions within their respective shells (Bash, Zsh, Ksh, etc.).

2. high level languages

Also many of the glibc libraries that are called by tools such as kill are exposed to higher level languages too, so you can create whatever tools you desire in languages such as Python, Perl, or Ruby.

3. chaining

This last method is probably the one that most people tend to use. You take the output from one command and use it together with another tool as input. The entire UI from the command line is built with this single purpose in mind. So there are many constructs at your disposal for doing exactly this.

$ kill $(pgrep myscript.sh)
$ find . -type f -exec ... {} +
$ ls -l | grep ...
$ find . -type f -print0 | xargs ....

The list goes on....

Your issue

So with the following command:

$ sudo kill $(fuser -n tcp <port_number> 2> /dev/null)

You could wrap this into a Bash function:

$ mykill () { sudo kill $(fuser -n tcp "$1" 2> /dev/null); }

Changing this slightly just so we can see what happens when we run it:

$ mykill () { echo "sudo kill \$(fuser -n tcp "$1" 2> /dev/null)"; }
$ mykill 123
sudo kill $(fuser -n tcp 123 2> /dev/null)

Since old times, UNIX philosophy has been "building short, simple, clear, modular, and extendable code", so a super-tool that kills based in port number is way too complicated to maintain, on the other hand, kill was meant to send signals to the process (which happens to end them) not necessarily "to kill" them.

I have searched for a tool that do that, none found that only do that (normally they comes bundled with other tools/functionality or are complex of use). Of course you can use kill -SIGNAL $(lsof -i:port) to send any signal you like.

  • You probably want to add the -terse option to lsof, so you get the port only as input for the kill command. – Moreaki Jun 3 '17 at 14:07

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.