Kill all processes running on port 8080 on Mavericks - terminal

I'm getting a "socket.error: No socket could be created" when running a web.py script.
Is there a way to kill all processes on running on port 8080 (or any other port I wish) with a single line in Terminal on OSX Mavericks?

It's a single line, but you'd need to put it into a shell alias or shell script in order to make it easy to use:
$ kill $(lsof -i tcp:8080 | tail -n +2 | awk '{ print $2 }')
If you want to see and kill processes that don't belong to you, then sudo needs to get involved:
$ sudo kill $(sudo lsof -i tcp:8080 | tail -n +2 | awk '{ print $2 }')

The best way to kill all proccesses running on port 8080 in ubuntu is:
sudo fuser -k 8080/tcp

Related

How do I not show the processes that I can't kill with 'kill + [pid number]' command?

I was working on a project "make a task manager in linux" at school
I used ps -u [username] -o stat,cmd --sort=-pid | awk '{print $2$3$4}' command to get cmd names from the ps command
If I use this command, I see the part of the result like this :
awk{print$2$3$4}
ps-u[username]
when I try to terminate those process using the pid of each process, it won't terminate them because their PID doesn't exist.
How could I not show those awk{print$2$3$4} and ps-u[username] ???
I couldn't think of any idea
ps -u [username] -o stat,cmd --sort=-pid | awk '{print $2$3$4}'
You can't kill them because they were only alive while the commands were running, which was the same command you used to generate that output.
There's a few ways you can suppress these. I think the easiest would be to filter them out in your awk script.:
ps -u [username] -o stat,cmd --sort=-pid | awk '$2!="awk" && $2!="ps"{print $2$3$4}'
JNevill's solution excludes every running awk or ps process. I think it's better to exclude processes on tty. Also, you aren't getting complete commands with how you use awk. I (kind of) solved it using sed.
$ ps -u $USER -o stat,tty,cmd --sort=-pid | grep -v `ps -h -o tty $$` | sed -r 's/.* (.*)$/\1/'
You can test it with the following command. I opened man ps in another terminal.
$ ps -u $USER -o stat,tty,cmd --sort=-pid | grep -v `ps -h -o tty $$` | grep -E '(ps|grep)'
S+ pts/14 man ps
The downside is, besides excluding ps and grep, it excludes your application as well.

Unable to stop appium server programmatically

I am using this command to stop the Appium server :
kill $(lsof -t -i: 10723)
But now it is not working.
Avoid hardcoding pid, port, address, etc. Search by process name (Appium):
kill -9 $(ps -A | grep -m1 Appium | awk '{print $1}')

How can I kill whatever process is using port 8080 so that I can vagrant up? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 11 months ago.
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
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?
This might help
lsof -n -i4TCP:8080
The PID is the second field in the output.
Or try:
lsof -i -P
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
Go to /usr/local/bin/ (Can use command+shift+g in finder)
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
Save this file.
Make the file executable chmod 755 stop
Now, go to terminal and write stop 8888 (or any port)
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.
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.
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
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.
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.
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
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
You can also use the Activity Monitor to identify and quit the process using the port.
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
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
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"
For me this worked
Open your mac terminal
kill $(lsof -t -i:8080)
Explanation:
lsof -t returns the PID and passes that to kill.
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"

Trying to kill process by shell scripting

I want to kill process through shell scripting it is giving me error. Here is what i tried so far:
When i try to kill memcache it give me error like "kill: No such process" , i used below command:
ps -ef | grep "memcache" | awk '{print $2}' | xargs kill;
or if try like below:
kill -9 $(pidof memcache)
i get error like below:": arguments must be process or job IDs"
When i run directly on command prompt process is running:
ring#ubuntu:~/parasol$ ps aux | grep memcache
memcache 873 0.0 0.0 323220 1188 ? Sl 22:56 0:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1
ring 1714 0.0 0.0 9384 920 pts/0 S+ 23:45 0:00 grep --color=auto memcache
I reff to https://askubuntu.com/questions/239923/shell-script-to-9-kill-based-on-name
AND
Shell script to capture Process ID and kill it if exist
My Ubuntu Version:
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.2 LTS"
NAME="Ubuntu"
VERSION="12.04.2 LTS, Precise Pangolin"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu precise (12.04.2 LTS)"
VERSION_ID="12.04"
Acutally
ps -ef | grep memcache
will give two lines..
so you can go this way
ps -ef | grep memcache | grep -v "grep" | awk '{print $2}' | xargs kill;
This can get you exact one PID
if I has to do I would break it in 2 lines in start
#!/bin/bash
PID=`ps -ef | grep memcache | grep -v "grep" | awk '{print $2}'`
echo $PID
#to check PID is right
kill -9 $PID
save it in scrip files say test.sh
then on terminal
chmod +x test.sh
then
./test.sh
You can use these commands:
pkill memcached
or
pgrep memcached | xargs kill -9
or
killall memcached.
pgrep, pkill - look up or signal processes based on name and other attributes.
Your first command might be killing itself before having chance to kill the target processes.
For a reliable way, run pkill /usr/bin/memcached or pkill -9 /usr/bin/memcached although the latter is a bad practice.
For the first command, you see that there are two processes that matches the pattern you grep for. The actual memcached process and your grep process. That is probably the reason for the error of the first command line.
Try narrowing the search down, for example by grepping for e.g. "^memcache.*/usr/bin/memcached".
The problem with the second error is that you're calling pidof with the username instead of the process name, so the command is essentially kill -9 without any process id. Try instead e.g. pidof memcached to get the process id of the correct process.
ps -ef | grep "memcache" | awk '{print $2}' | xargs kill;
PID=`ps -ef | grep memcache | grep -v "grep" | awk '{print $2}'`
#...
First, you could use cut instead of awk in this case. No need to use a tank to kill a fly ;)
Then, why is it necessary to brutally kill memcache? You have a daemon to stop it :
/etc/init.d/memcached stop
service memcached stop

Getting PID of sshd

I am executing sshd in a bash script using
$ /usr/sbin/sshd
How do I get the process ID of this sshd that I executed?
sshd will typically write a PID file; by default this is at /var/run/sshd.pid. You can use this to find the process ID of the listening sshd process. You should be aware that sshd may fork several subprocesses as it works, so what you want really depends on what you intend to do with it.
Try this command:
ps aux | grep -e /usr/sbin/sshd | grep -v grep | tr -s " " | cut -d " " -f2
or
cat /var/run/sshd.pid

Resources