Latest Posts

Friday, March 3, 2017

LINUX :: Kill a Process Id by looking up the port

We might always feel the need to find the process running on a specific port. 

A classic example would be a default installation of Apache Tomcat which might not run successfully (i.e. default port 8080). One of the probability is that there might be already a process running on port 8080.

Following are some ways to find the port number for the relevant process



  1. netstat – a command-line tool that displays network connections, routing tables, and a number of network interface statistics.
  2. fuser – a command line tool to identify processes using files or sockets.
  3. lsof – a command line tool to list open files under Linux / UNIX to report a list of all open files and the processes that opened them.
  4. /proc/$pid/ file system – Under Linux /proc includes a directory for each running process (including kernel processes) at /proc/PID, containing information about that process, notably including the processes name that opened port.
Please run the following commands as root user (ie. "sudo")

netstat -tulpn | grep :8080

Output
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1689/apacheSample
So the process id found is PID --> 1689
Linux and Unix-like operating system supports the standard terminate signals listed below:
  1. SIGHUP (1) – Hangup detected on controlling terminal or death of controlling process. Use SIGHUP to reload configuration files and open/close log files.
  2. SIGKILL (9) – Kill signal. Use SIGKILL as a last resort to kill process. This will not save data or cleaning kill the process.
  3. SIGTERM (15) – Termination signal. This is the default and safest way to kill process.

What Linux or Unix permissions do I need to kill a process?

Rules are simple:
  1. You can kill all your own process.
  2. Only root user can kill system level process.
  3. Only root user can kill process started by other users.
sudo kill <pid>
Example ::
sudo kill 1689
OR
sudo kill -15 1689
OR
sudo kill -9 1689

No comments:

Post a Comment