Have you ever encountered issues starting a server or application because the required port is already in use? This common scenario can occur when a process doesn’t release its port properly—
perhaps due to a dropped SSH connection or an application crash. This guide provides detailed instructions for identifying and terminating processes holding onto specific ports across Windows, Linux, and macOS.
When an application runs, it binds to a specific port for network communication. If the application fails to release the port upon termination, the port remains occupied until one of the following occurs:
Knowing how to manually kill these processes is crucial for minimizing downtime and avoiding unnecessary system restarts.
Read: How to Close Ports on Linux and Windows Systems
8080
with your port number): netstat -ano | findstr :8080
The output will display the PID (e.g., TCP 0.0.0.0:8080 ... LISTENING 1234
).
taskkill /PID 1234 /F
The /F
flag forces termination. Re-run the netstat command to confirm the port is free.
For Windows 10 and later, you can use a one-liner in PowerShell:
Stop-Process -Id (Get-NetTCPConnection -LocalPort 8080).OwningProcess -Force
Alternatively:
Get-Process -Id (Get-NetTCPConnection -LocalPort 8080).OwningProcess | Stop-Process
Read: An introduction to Windows PowerShell for beginners
If you have npm installed, run:
npx kill-port 8080
This utility automatically identifies and terminates the process using the specified port.
netstat -ano | findstr :8080
tskill
: tskill 1234
taskkill //PID 1234 //F
Create a batch file (killport.bat
) with the following script:
@ECHO OFF
set /P port="Enter port : "
echo Showing process running with port %port%
netstat -ano | findstr "PID :%port%"
set /P pid="Enter PID to kill : "
taskkill /pid %pid% /f
set /P exit="Press any key to exit..."
Add the file’s location to your PATH for system-wide access.
Read: Repeat Linux Commands Every X Seconds for Real-Time Monitoring, Automation & Precise Scheduling
sudo lsof -i :8080
sudo kill -9 1234
sudo kill -9 $(sudo lsof -t -i:8080)
Read: How to Kill Processes in Linux: Beginner-Friendly Guide to Command Line Termination
Find and kill the process in one command:
sudo fuser -k 8080/tcp
sudo netstat -tulpn | grep :8080
sudo kill -9 1234
lsof -i :8080
kill -9 1234
kill -9 $(lsof -ti:8080)
If a process refuses to terminate:
taskkill /PID 1234 /F /T
to also terminate child processes.sudo kill -9 1234
with elevated privileges.If the port stays occupied, it may be due to the socket entering a TIME_WAIT state. Wait 1-2 minutes or configure your application to use the SO_REUSEADDR
option.
If multiple processes share the port, identify and terminate them individually:
sudo lsof -i :8080 | awk '{print $2}' | xargs sudo kill -9
-9
).netstat
helps diagnose network-related issues.Yes, forcefully terminating a process (using SIGKILL or the /F
flag) does not allow it to perform cleanup operations, which can result in data loss if the process was writing data.
Review the process name along with its PID using commands like tasklist
(Windows) or ps aux
(Linux/macOS) before terminating.
This often occurs because the previous instance did not properly release the port, leaving it in a TIME_WAIT state.
Yes, most applications can be configured to use specific ports. On Linux, systemd socket activation can also manage port allocation.
Modern frameworks and servers typically implement signal handling to release ports on termination. For custom applications, ensure proper shutdown hooks are in place.
Windows: netstat -an
Linux/macOS: netstat -tuln
Effectively managing processes that occupy specific ports is a vital skill for developers and system administrators. This guide has detailed methods for Windows, Linux, and macOS to help you quickly identify and terminate problematic processes. Always exercise caution and verify before killing processes, especially on production systems.
By following these approaches, you can resolve port conflicts efficiently and maintain a stable development environment.
The post How to Kill Processes Using Specific Ports on Linux, Windows and MacOS appeared first on net2.
In Linux environments, system administrators need to continuously monitor log files to evaluate system health,…
The landscape of artificial intelligence is rapidly evolving, demanding robust and scalable infrastructure. To meet…
We’re pleased to release Ubuntu Security Guide profiles for CIS benchmarks. These profiles will allow…
Memory leaks are among the most frustrating bugs to track down in C and C++…
When upgrading to Ubuntu 22.04 LTS (Jammy Jellyfish), many users encounter the error message: “Although…
The landscape of generative AI is rapidly evolving, and building robust, scalable large language model…