125

On MacOSX, I'm using Packer to build a Vagrant box so I need to continually bring it up and tear it down. I'm attempting to 'vagrant up', and receive the standard error because the port is in use:

"Vagrant cannot forward the specified ports on this VM, since they would collide with some other application that is already listening on these ports. The forwarded port to 8080 is already in use on the host machine."

The solution seems simple enough: I just need to identify the process that is holding port 8080 open and kill that process, right?. It's not that easy.


If I run the command:

nmap localhost -p 8080

I receive the following output:

PORT     STATE SERVICE
8080/tcp open  http-proxy

If I run the following command:

top -o prt

The highest port in use in 1360


If I run the following command:

 netstat -tulpn | grep :8080

I receive:

netstat: n: unknown or uninstrumented protocol

If I run the following command:

lsof -i :8080

I receive no output


If I restart my computer, the port is now available and I can now 'vagrant up'.

How can I kill whatever process is using port 8080 so that I can vagrant up without restarting my computer?

Richard
  • 1,224
  • 3
  • 16
  • 32
Jason Curran
  • 1,385
  • 2
  • 10
  • 7

15 Answers15

209

This might help

lsof -n -i4TCP:8080 

The PID is the second field in the output.

Or try:

lsof -i -P
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Do you happen to have an HP LaserJet printer? http://blog.hgomez.net/blog/2010/09/25/how-to-fix-hp-laserjet-1102w-printeragent-using-port-8080-on-mac/ – Mark Setchell Jun 24 '14 at 13:56
  • 4
    Thanks Mark! For whatever reason, after restarting my computer, the lsof commands were outputting nothing, and now they are. Now, all 3 of the of the lsof commands are working and I can identify the process id and kill it with: sudo kill -9 000 ... Where 000 is the process id. The name of the process is "VBoxHeadless". If I get more information on why it was not working previously I will report back. FYI: I have no printer attached. – Jason Curran Jun 24 '14 at 14:26
  • Try `sudo lsof -n -i4TCP:8080` to pick up processes owned by root. – mpelzsherman Jul 02 '19 at 03:01
143

Fast and quick solution:

lsof -n -i4TCP:8080

PID is the second field. Then, kill that process:

kill -9 PID

Less fast but permanent solution

  1. Go to /usr/local/bin/ (Can use command+shift+g in finder)

  2. Make a file named stop. Paste the below code in it:

#!/bin/bash
touch temp.text
lsof -n -i4TCP:$1 | awk '{print $2}' > temp.text
pidToStop=`(sed '2q;d' temp.text)`
> temp.text
if [[ -n $pidToStop ]]
then
kill -9 $pidToStop
echo "Congrates!! $1 is stopped."
else
echo "Sorry nothing running on above port"
fi
rm temp.text
  1. Save this file.
  2. Make the file executable chmod 755 stop
  3. Now, go to terminal and write stop 8888 (or any port)
Leon Bartz
  • 63
  • 8
ronak kothari
  • 1,430
  • 1
  • 9
  • 7
  • 4
    I tried the permanent solution but got 'Permission denied' when attempting to run the script. Solved by first running `chmod 755 stop`. – nomadoda Nov 05 '18 at 12:15
40

In case above-accepted answer did not work, try below solution. You can use it for port 8080 or for any other ports.

sudo lsof -i tcp:3000 

Replace 3000 with whichever port you want. Run below command to kill that process.

sudo kill -9 PID

PID is process ID you want to kill.

Below is the output of commands on mac Terminal.

Command output

Akshay Thorve
  • 678
  • 6
  • 10
  • 2
    i'd try regular kill (not -9) first. The -9 option could result in the socket not being closed properly. The process may be killed but another listener will forked in its place. – stackPusher Jan 28 '19 at 21:08
8

Use the following command:

lsof -n -i4TCP:8080 | awk '{print$2}' | tail -1 | xargs kill -9

The process id of port 8080 will be picked and killed forcefully using kill -9.

sytolk
  • 7,223
  • 3
  • 25
  • 38
Suniti
  • 81
  • 1
  • 1
7

I needed to kill processes on different ports so I created a bash script:

killPort() {
  PID=$(echo $(lsof -n -i4TCP:$1) | awk 'NR==1{print $11}')
  kill -9 $PID
}

Just add that to your .bashrc and run it like this:

killPort 8080

You can pass whatever port number you wish

Curt
  • 794
  • 7
  • 8
6

To script this:

pid=$(lsof -ti tcp:8080)
if [[ $pid ]]; then
  kill -9 $pid
fi

The -t argument makes the output of lsof "terse" which means that it only returns the PID.

voutasaurus
  • 2,968
  • 2
  • 24
  • 37
6
sudo lsof -i:8080

By running the above command you can see what are all the jobs running.

kill -9 <PID Number>

Enter the PID (process identification number), so this will terminate/kill the instance.

Viswesvar Sekar
  • 2,743
  • 3
  • 11
  • 7
4

I needed to run this command

sudo lsof -i :80 # checks port 8080

Then i got

COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
acwebseca 312 root   36u  IPv4 0x34ae935da20560c1      0t0  TCP 192.168.1.3:50585->104.25.53.12:http (ESTABLISHED)

show which service is using the PID

ps -ef 312

Then I got this

  UID   PID  PPID   C STIME   TTY           TIME CMD
    0   312    58   0  9:32PM ??         0:02.70 /opt/cisco/anyconnect/bin/acwebsecagent -console

To uninstall cisco web security agent run

sudo /opt/cisco/anyconnect/bin/websecurity_uninstall.sh

credits to: http://tobyaw.livejournal.com/315396.html

mjabadilla
  • 1,006
  • 14
  • 31
3

It can be Cisco AnyConnect. Check if /Library/LaunchDaemons/com.cisco.anyconnect.vpnagentd.plist exists. Then unload it with launchctl and delete from /Library/LaunchDaemons

1

You can also use the Activity Monitor to identify and quit the process using the port.

gcmori
  • 11
  • 1
  • 1
    How would I do that? – Emil Stenström Nov 15 '17 at 19:53
  • On MacOSX: Activity Monitor > Network > sort by PID > find your blocked port (eg 8080, 5000 etc) > click top left 'X' > confirm you want to quit the process. May be a good idea to edit your run/build configuration to ensure only single server instances can be created, ie your used ports are freed on teardown. – Alex Friedmann Jul 01 '19 at 13:24
1

Run: nmap -p 8080 localhost (Install nmap with MacPorts or Homebrew if you don't have it on your system yet)

Nmap scan report for localhost (127.0.0.1)

Host is up (0.00034s latency).

Other addresses for localhost (not scanned): ::1

PORT STATE SERVICE

8080/tcp open http-proxy

Run: ps -ef | grep http-proxy

UID PID PPID C STIME TTY TIME CMD

640 99335 88310 0 12:26pm ttys002 0:00.01 grep http-proxy"

Run: ps -ef 640 (replace 501 with your UID)

/System/Library/PrivateFrameworks/PerformanceAnalysis.framework/Versions/A/XPCServices/com.apple.PerformanceAnalysis.animationperfd.xpc/Contents/MacOS/com.apple.PerformanceAnalysis.animationperfd

Port 8080 on mac osx is used by something installed with XCode SDK

Community
  • 1
  • 1
Yiling
  • 2,817
  • 2
  • 21
  • 27
1

try netstat

netstat -vanp tcp | grep 3000

if your netstat doesn't support -p , use lsof

sudo lsof -i tcp:3000 

For Centos 7 use

netstat -vanp --tcp | grep 3000
Khem Raj Regmi
  • 2,130
  • 19
  • 21
0

After referring to the solution of @voutasaurus. I wrote this utility to simplify the process of killing all the processes that are running on the port.

killProcessesUsing3000 () {
  pid=$(lsof -ti :3000)  # The -t argument makes the output of lsof "terse" (Brief) which means that it only returns the PID.
  # PID contains process processes that run on the 3000 port. In new lines if they are multiples
  for num ($=pid) {
    echo $num
    kill -9 $num
  }
}

#Alias
alias kill3000="killProcessesUsing3000"
Aswin Prasad
  • 407
  • 6
  • 14
0

For me this worked

Open your mac terminal

kill $(lsof -t -i:8080)

Explanation:

lsof -t returns the PID and passes that to kill.

Harsh
  • 2,893
  • 29
  • 16
0

I tried many of the above and they didn't work for me.

After many hours, I found this one liner:

# kill 8080
alias nuke88="lsof -i tcp:8080 | grep LISTEN | awk '{print \$2}' | xargs kill"

# kill 3000
alias nuke3k="lsof -i tcp:3000 | grep LISTEN | awk '{print \$2}' | xargs kill"
FBB
  • 326
  • 1
  • 12