How to kill a process running on particular port in Linux?
How to Kill a Process Running on a Particular Port in Linux?
So, you've encountered a common problem - your Tomcat server didn't shut down properly, and now you're unable to restart it because the process is still running on port 8080. Don't worry, we've got you covered! In this blog post, we'll walk you through the steps to kill a process running on a specific port in Linux.
Step 1: Find the Process ID (PID) of the Running Process
The first step is to find the Process ID (PID) of the process running on port 8080. We can use the lsof
command to list all the processes listening on a specific port. Open your terminal and run the following command:
lsof -i :8080
This will display the details of all the processes running on port 8080, including the PID.
Step 2: Kill the Process
Once you've obtained the PID of the process running on port 8080, you can kill it using the kill
command. Run the following command in your terminal:
kill <PID>
Replace <PID>
with the actual PID you obtained from the previous step. This command sends a termination signal to the process, forcing it to shut down.
Step 3: Verify the Process has Been Killed
To ensure that the process has been successfully terminated, you can run the lsof
command again:
lsof -i :8080
If no processes are displayed, then congratulations! You've successfully killed the process running on port 8080.
Going the Extra Mile: Automate the Process Killing
If you find yourself frequently needing to kill processes running on specific ports, you can create a shell script to automate the process. Here's an example:
#!/bin/bash
PORT=$1
if [[ -z $PORT ]]; then
echo "Please provide a port number as an argument."
exit 1
fi
PID=$(lsof -t -i :$PORT)
if [[ -z $PID ]]; then
echo "No process found running on port $PORT."
exit 1
fi
kill $PID
echo "Process with PID $PID killed successfully."
Save the above script into a file, e.g., killport.sh
. Then, you can run it with the desired port number as an argument:
./killport.sh 8080
This script will automatically find the process running on the specified port and kill it for you.
Conclusion
Killing a process running on a particular port in Linux is a straightforward task once you know the right commands. By following the steps outlined in this guide, you can now easily terminate processes that are causing issues and get your systems back on track.
So go ahead, give it a try! And if you found this guide helpful, be sure to share it with your friends or colleagues who may also benefit from this knowledge. 💪🔥
Let us know in the comments below if you've encountered any other Linux-related problems and would like us to cover them in future blog posts. Happy process killing! 💻🔪💥