Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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:

http://stackoverflow.com/questions/11583562/how-to-kill-process-running-on-particular-port-in-linux

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?

share|improve this question
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? –  Stephane Chazelas Feb 20 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. –  909 Niklas Feb 21 at 18:15
add comment

2 Answers

up vote 5 down vote accepted

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)
share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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