shell script to kill the process listening on port 3000?
alias kill3000="fuser -k -n tcp 3000"
How to kill a process running on particular port in Linux?
Use the command
sudo netstat -plten |grep java
used grep java
as tomcat
uses java
as their processes.
It will show the list of processes with port number and process id
tcp6 0 0 :::8080 :::* LISTEN
1000 30070621 16085/java
the number before /java
is a process id. Now use kill
command to kill the process
kill -9 16085
-9
implies the process will be killed forcefully.
How do I kill the process currently using a port on localhost in Windows?
Step 1:
Open up cmd.exe (note: you may need to run it as an administrator, but this isn't always necessary), then run the below command:
netstat -ano | findstr :<PORT>
(Replace <PORT>
with the port number you want, but keep the colon)
The area circled in red shows the PID (process identifier). Locate the PID of the process that's using the port you want.
Step 2:
Next, run the following command:
taskkill /PID <PID> /F
(No colon this time)
Lastly, you can check whether the operation succeeded or not by re-running the command in "Step 1". If it was successful you shouldn't see any more search results for that port number.
Windows Kill Process By PORT Number
Solution 1: Kill Process
Run command-line as an Administrator
netstat -ano | findstr :<yourPortNumber>
taskkill /PID <typeyourPIDhere> /F
Solution 2: Change Port
Please Make sure that new port you are going to set for your Application doesn't listen to any other process
Change the port
server.port=8088 # Server HTTP port.
Solution 3:
Another way is to terminate the process (in IDE) and clean and rebuild project.
UPDATE:
For solution 2, Please Make sure that new port you are going to set for your Application doesn't listen to any other process.
How to check Port Status?
Option 1
Run resmon.exe
and go to Network -> Listening Port
(Also can be viewed on TaskManager)
Option 2
PowerShell
Get-Process -Id (Get-NetTCPConnection -LocalPort portNumber).OwningProcess
cmd
C:\> netstat -a -b
(Add -n to stop it trying to resolve hostnames, which will make it a lot faster.)
-a
Displays all connections and listening ports.
-b
Displays the executable involved in creating each connection or listening port. In some cases, well-known executables host multiple independent components, and in these cases, the sequence of components involved in creating the connection or listening port is displayed. In this case, the executable name is in [] at the bottom, on top is the component it called, and so forth until TCP/IP was reached. Note that this option can be time-consuming and will fail unless you have sufficient permissions.
-n
Displays addresses and port numbers in numerical form.
-o
Displays the owning process ID associated with each connection.
Kill a Process by Looking up the Port being used by it from a .BAT
Here's a command to get you started:
FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :8080') DO @ECHO TaskKill.exe /PID %%P
When you're confident in your batch file, remove @ECHO
.
FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :8080') DO TaskKill.exe /PID %%P
Note that you might need to change this slightly for different OS's. For example, on Windows 7 you might need tokens=5
instead of tokens=4
.
How this works
FOR /F ... %variable IN ('command') DO otherCommand %variable...
This lets you execute command
, and loop over its output. Each line will be stuffed into %variable
, and can be expanded out in otherCommand
as many times as you like, wherever you like. %variable
in actual use can only have a single-letter name, e.g. %V
.
"tokens=4 delims= "
This lets you split up each line by whitespace, and take the 4th chunk in that line, and stuffs it into %variable
(in our case, %%P
). delims
looks empty, but that extra space is actually significant.
netstat -a -n -o
Just run it and find out. According to the command line help, it "Displays all connections and listening ports.", "Displays addresses and port numbers in numerical form.", and "Displays the owning process ID associated with each connection.". I just used these options since someone else suggested it, and it happened to work :)
^|
This takes the output of the first command or program (netstat
) and passes it onto a second command program (findstr
). If you were using this directly on the command line, instead of inside a command string, you would use |
instead of ^|
.
findstr :8080
This filters any output that is passed into it, returning only lines that contain :8080
.
TaskKill.exe /PID <value>
This kills a running task, using the process ID.
%%P instead of %P
This is required in batch files. If you did this on the command prompt, you would use %P
instead.
Finding the PID listening to a port
Try this:
pid=$(sudo lsof -i :3001 -t)
kill $pid
Related Topics
Removing Sensitive Data from Git. "Fatal: Ambiguous Argument 'Rm'"
Linux/Gcc: Ldd Functionality from Inside a C/C++ Program
Setting Up a Cronjob on Google Compute Engine
Dropping of Connections with Tcp_Tw_Recycle
How to Capture All of My Compiler's Output to a File
What's the Best Way to Distribute a Binary Application for Linux
Rm: Cannot Remove: Permission Denied
Access Permissions of /Dev/Mem
Linux Bash: Setting Iptables Rules to Allow Both Active and Passive Ftp
How to Ignore Some Differences in Diff Command
Convert an Iso Date to Seconds Since Epoch in Linux Bash
Bash: Run an Executable File in Background
How Do 2 or More Fork System Calls Work
Sh: 0: Getcwd() Failed: No Such File or Directory on Cited Drive
How to Upgrade Openssl in Centos 6.5/Linux/Unix from Source
How to Find Files That Only Have Certain Permission for Owner