Showing posts with label kill command. Show all posts
Showing posts with label kill command. Show all posts

Sunday, September 10, 2017

How To Find And Kill Application / Process Running On Particular Port In Linux Systems

How To Find And Kill Application / Process Running On Particular Port In Linux Systems



Today, I'm going to show you how to find and kill application / process running on particular port in Linux Systems.

We can use lsof command to find the process running on particular port. The syntax of lsof command to find the application running on particular port looks like following..

lsof -i:port

Summary of above code...
lsof command is used to list open files.. and the argument -i is used to list files whose ip address / port matched to specified ip address / port.

Note: If you ignore ip address / port after -i then the above command will lists processes for all ip addresses...

For example to find the process / application listening on port 80, run the following code..

lsof -i :80

The output will looks like following..

shivaraj@shivaraj-A14RM0E:~$ lsof -i:80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME plugin_ho 3981 shivaraj 20u IPv4 420822 0t0 TCP 192.168.0.117:33104->45.55.41.223:http (CLOSE_WAIT) shivaraj@shivaraj-A14RM0E:~$

If you omit port value... lsof -i the output will looks like following one..

shivaraj@shivaraj-A14RM0E:~$ lsof -i COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME plugin_ho 3981 shivaraj 20u IPv4 420822 0t0 TCP 192.168.0.117:33104->45.55.41.223:http (CLOSE_WAIT) node 4165 shivaraj 12u IPv4 509873 0t0 TCP *:http-alt (LISTEN) firefox 4178 shivaraj 114u IPv4 7216095 0t0 TCP 192.168.0.117:42436->server-54-230-190-100.maa3.r.cloudfront.net:https (ESTABLISHED) shivaraj@shivaraj-A14RM0E:~$

Now, we are going to see how to kill the process which is listening on particular port.. If we can find the pid of process we can pipe it to kill command. so that we cann kill the process listening on particular port..

By specifying -t for lsof command we can get pid of process listening on particular port... Look at the below command signature..

lsof -t -i :port

If you want to find out pid of process listening on port 80, run the following code..

lsof -t -i :80

The above commands output will looks like..

shivaraj@shivaraj-A14RM0E:~$ lsof -t -i :80 3981 shivaraj@shivaraj-A14RM0E:~$

To kill the process listening on port 80, pass the output of above command to kill command...

sudo kill $(lsof -t -i :80)

That's it for now.. If you like don't forget to share it guys.. You can follow us on fb.com/opensourceinside and also subscribe our channel on Youtube..


Friday, June 5, 2015

How to Kill Linux Processes/Unresponsive Applications Using 'kill & pkill' Command

How to Kill Linux Processes/Unresponsive Applications Using 'kill & pkill' Command


kill process in linux

How do we kill a resource/process in Linux?

Obviously we find the PID of the resource and then pass the PID to the kill command. Speaking more accurately, we can find PID of a resource (say firefox) as:

Syntax :

ps -A | grep -i process name

e.g.

ps -A | grep -i firefox

or

alternatively we can use pgrep command to find PID

Syntax :

pgrep process name

e.g.

pgrep firefox

sample output:

sample output image not displyed

In the above output, the number '2697' is the PID of process (firefox), use the kill command to kill the process as shown below.

Syntax :

kill PID OR kill -signalName PID OR kill -signalNumber PID

Where,

  1. signalNumber : A non-negative decimal integer, specifying the signal to be sent instead of the default TERM.
  2. signalName : A symbolic signal name specifying the signal to be sent instead of the default TERM.

e.g.

kill 2697

The kill command sends a signal to a process, whose PID is passed along with the command.

SIGNAL NUMBERSIGNAL NAMEDescriptionUsed for
0SIGNULL (NULL)NullCheck access to pid
1SIGHUP (HUP)HangupTerminate; can be trapped
2SIGINT (INT)InterruptTerminate; can be trapped
3SIGQUIT (QUIT)QuitTerminate with core dump; can be trapped
9SIGKILL (KILL)KillForced termination; cannot be trapped
15SIGTERM (TERM)TerminateTerminate; can be trapped
24SIGSTOP (STOP)StopPause the process; cannot be trapped. This is default if signal not provided to kill command.
25SIGTSTP (STP)TerminalStop/pause the process; can be trapped
26SIGCONT (CONT)ContinueRun a stopped process

Alternatively, we can use pkill command, which kills a process based upon name and other attributes of a process. To kill a process say whose name is firefox, we need to execute:

Syntax :

pkill process name

e.g.

pkill firefox

Forced Killing (SIGKILL or kill -9)

The kill command has a misleading name because it does not actually kill processes. Rather, it sends signals to them. Each process is supplied with a set of standard signal handlers by the operating system in order to deal with incoming signals. When no signal is explicitly included in the command, signal 15, named SIGTERM, is sent by default. If this fails, the stronger signal 9, called SIGKILL, should be used. For example, the following command would nearly guarantee that process 485 would be killed:

Syntax :

kill -9 PID OR pkill -9 process name

kill -9 2697 OR pkill -9 firefox

The only situation in which signal 9 will fail is if the process is in the midst of making a system call, which is a request to the kernel (i.e., the core of the operating system) for some action such as process creation. In this situation, the process will die once it has returned from the system call.

To list the signal names, pass the -l option as follows:

There are more than 60 signals that can be used with kill, but, fortunately, most users will only need to be aware of signal 9. The full list is contained in the file /usr/include/linux/signal.h and can be viewed by using kill with its -l option, i.e.,

kill -l