fuser : Simplifying Process Termination by Port Number
There are several ways to kill a process, but among them, if you know the port number of the process,
I am introducing a simple way to quit using the fuser command.
fuser : Basic info
fuser is a command that tells which process is using a file or socket.
info fuser
If you pass a file or directory to fuser without any options, the process ID using that resource will be output.
$ sudo fuser /var/lib/mysql/mysql.sock
/var/lib/mysql/mysql.sock : 20309
fuser : Basic usage
e.g) When forcefully terminating a process using port 20001
fuser -k 20001/tcp
The kill command requires knowledge of the PID (process ID) to use, but fuser is useful when you do not know the PID but know the port number of the service.
You can see the difference by comparing it to the process of terminating using the kill command below.
fuser : When only the port number is known, the process of terminating with the kill command
Step 1. Search for the known port number to find the process ID (PID)
netstat -tnlp|grep 20001
(Here, 20001 is the known port number for which you want to find the corresponding PID, and the result is assumed to be that the PID is 1234)
Step 2. Force quit the process
kill -9 1234
1234 is the PID (process ID) found through step 1, and -9 is an option used when you want to terminate by directly specifying the process ID.
Another way (one line command)
Use “gawk” to retrieve the process from “netstat” results and pass it to the kill command.
kill -9 `netstat -tnlp|grep 20001|gawk '{ print $7 }'|grep -o '[0-9]*'`
Another Command : lsof
You can also use “lsof” to achieve the desired result.
$ sudo lsof /var/lib/mysql/mysql.sock
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 20309 mysql 26u unix 0xffff88803f457400 0t0 206420644 /var/lib/mysql/mysql.sock
Reference
Discover more from Bricoleur88 Tech Insight
Subscribe to get the latest posts sent to your email.