How to kill a process running on particular port in Linux?

Cover Image for How to kill a process running on particular port in Linux?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

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! 💻🔪💥


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello