Determine the process pid listening on a certain port - unix

As the title says, I'm running multiple game servers, and every of them has the same name but different PID and the port number. I would like to match the PID of the server which is listening on certain port, and then I would like to kill this process. I need that in order to complete my bash script.
Is that even possible? Because it didn't find yet any solutions on the web.

Short version which you can pass to kill command:
lsof -i:80 -t

The -p flag of netstat gives you PID of the process:
netstat -l -p
*use sudo if showing - instead of PID
Edit: The command that is needed to get PIDs of socket users in FreeBSD is sockstat.
As we worked out during the discussion with #Cyclone, the line that does the job is:
sockstat -4 -l | grep :80 | awk '{print $3}' | head -1

netstat -p -l | grep $PORT and lsof -i :$PORT solutions are good but I prefer fuser $PORT/tcp extension syntax to POSIX (which work for coreutils) as with pipe:
pid=`fuser $PORT/tcp`
it prints pure pid so you can drop sed magic out.
One thing that makes fuser my lover tools is ability to send signal to that process directly (this syntax is also extension to POSIX):
$ fuser -k $port/tcp # with SIGKILL
$ fuser -k -15 $port/tcp # with SIGTERM
$ fuser -k -TERM $port/tcp # with SIGTERM
Also -k is supported by FreeBSD: http://www.freebsd.org/cgi/man.cgi?query=fuser

netstat -nlp should tell you the PID of what's listening on which port.

Syntax:
kill -9 $(lsof -t -i:portnumber)
Example:
To kill the process running at port 4200, run following command
kill -9 $(lsof -t -i:4200)
Tested in Ubuntu.

Since sockstat wasn't natively installed on my machine I hacked up stanwise's answer to use netstat instead..
netstat -nlp | grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:2000" | awk '{print $7}' | sed -e "s/\/.*//g""

I wanted to programmatically -- using only Bash -- kill the process listening on a given port.
Let's say the port is 8089, then here is how I did it:
badPid=$(netstat --listening --program --numeric --tcp | grep "::8089" | awk '{print $7}' | awk -F/ '{print $1}' | head -1)
kill -9 $badPid
I hope this helps someone else! I know it is going to help my team.

on windows, the netstat option to get the pid's is -o and -p selects a protocol filter, ex.:
netstat -a -p tcp -o

Related

Docker: unexpected error (Failure EADDRINUSE)

I'm really new to Docker. I'm trying to run Wordpress, and I've run into an error.
$ docker-compose up -d
testpublichtml_mariadb_1 is up-to-date
Starting 00b4dc8e3264_testpublichtml_wordpress_1
ERROR: for wordpress Cannot start service wordpress: driver failed programming external connectivity on endpoint
00b4dc8e3264_testpublichtml_wordpress_1 (63165c221c0b2b11d513e97d35afa39146790086115029b9bb229212d0c8c06a): Error starting userland proxy: Bind for 0.0.0.0:80: unexpected error (Failure EADDRINUSE)
ERROR: Encountered errors while bringing up the project.
$
My guess is to try and check if something is on port 80, though I'm not sure how to check that.
When I enter netstat -tulnp | grep ':80', I get:
$ netstat -tulnp | grep ':80'
netstat: option requires an argument -- p
Usage: netstat [-AaLlnW] [-f address_family | -p protocol]
netstat [-gilns] [-f address_family]
netstat -i | -I interface [-w wait] [-abdgRtS]
netstat -s [-s] [-f address_family | -p protocol] [-w wait]
netstat -i | -I interface -s [-f address_family | -p protocol]
netstat -m [-m]
netstat -r [-Aaln] [-f address_family]
netstat -rs [-s]
Probably you have some service running on port 80. To check this, execute the following command.
netstat -tulnp | grep ':80'
The last column is PID/Program name of your process. If you want to kill it, use the following command.
kill PID
After that, you should be able to start your container.

How to kill process inside container? Docker top command

I have simple example from official guide at docker website.
I run the following:
sudo docker run -d ubuntu:latest /bin/sh -c "while true; do echo hello world; sleep 1; done"
a66asdasdhqie123...
Then take some output from created container:
sudo docker logs a66
hello
hello
hello
...
Then I lookup the running processes of a container:
sudo docker top a66
UID PID PPID C STIME TTY TIME CMD
root 25055 15152 0 20:07 ? 00:00:00 /bin/sh -c while true; do echo hello world; sleep 1; done
root 25295 25055 0 20:10 ? 00:00:00 sleep 1
Next I try to kill the first process of container:
sudo docker exec a66 kill -9 25055
However after I make it nothing changes. Process still works and output "hello" every second. What do I wrong?
When I reproduce your situation I see different PIDs between docker top <container> and docker exec -it <container> ps -aux. When you do docker exec the command is executed inside the container => should use container's pid. Otherwise you could do the kill without docker straight from the host, in your case: sudo kill -9 25055.
check this:
ps | grep -i a66 | tr -s ' '|cut -f2 -d' '|
{
while read line;
do kill -9 $line;
done
}
to understand this start from executing commands from left till end of each pipe (|)
Simpler option:
kill $(pidof a66)
Took me a while to find the right answer, but you will have to manage this process from within the container. When you run docker top a66 from the host, the PIDs are from your host, although that's not quite the case if using Cygwin. Regardless, you will need to bash or what-have-you back into your container and use commands like ps aux and kill while in the container to find and manage the different PIDs for the same processes there.
i was looking for something like this, but i couldn't find and then i did this:
[root#notebook ~]# docker exec -it tadeu_debian ps aux | grep ping |
awk '{ print $2 }' | xargs -I{} docker exec -i tadeu_debian kill -9
It was two "execs" from Docker e one xargs.
Well, i hope this helps someone!
when you build Docker, use this command :
RUN apt-get install lsof
then in py file you can use:
os.system("lsof /dev/nvidia* | awk '{print $2}' | xargs -I {} kill {}")
REMEMBER: this command kill all process on GPU

How to debug php-fpm performance?

Webpages are loading very slow, it takes them around 6 seconds to even start sending the page data, which is then sent in a matter of 0.2 seconds and is generated in 0.19 seconds.
I doubt that it is caused by php or the browser, so the problem must be with the server which is handled by nginx and php5-fpm
A server admin said that indeed the problem was caused by a misconfigured fpm or nginx
How can I debug the cause of the slowdown?
Setup: php5.3, mysql5, linux, nginx, php5-fpm
This question is probably too broad for StackOverflow, as the question could span several pages and topics.
However if the question was just how do I do debug the performance of PHP-FPM then the answer would be much easier - use Strace and the script below.
#!/bin/bash
mkdir trc
rm -rf trc/*.trc
additional_strace_args="$1"
MASTER_PID=$(ps auwx | grep php-fpm | grep -v grep | grep 'master process' | cut -d ' ' -f 7)
summarise=""
#shows total of calls - comment in to get
#summarise="-c"
nohup strace -r $summarise -p $MASTER_PID -ff -o ./trc/master.follow.trc >"trc/master.$MASTER_PID.trc" 2>&1 &
while read -r pid;
do
if [[ $pid != $MASTER_PID ]]; then
#shows total of calls
nohup strace -r $summarise -p "$pid" $additional_strace_args >"trc/$pid.trc" 2>&1 &
fi
done < <(pgrep php-fpm)
read -p "Strace running - press [Enter] to stop"
pkill strace
That script will attach strace to all of the running php-fpm instances. Any requests to the web server that reach php-fpm will have all of their system calls logged, which will allow you to inspect which ones are taking the most time, which will allow you to figure out what needs optimising first.
On the other hand, if you can see from the Strace output that PHP-FPM is processing each request fast, it will also allow you to eliminate that as the problem, and allow you to investigate nginx, and how nginx is talking to PHP-FPM, which could also be the problem.
#Danack saved my life.
But I had to change the command to get the MASTER_PID:
MASTER_PID=$(ps auwx | grep php-fpm | grep -v grep | grep 'master process' | sed -e 's/ \+/ /g' | cut -d ' ' -f 2)

uniprocessor or multiprocessor

On unix, how could we know whether the system is multiprocessor or uniprocessor?
Some times we have to answer owr own question :)
On Solaris run the command
/usr/sbin/psrinfo -v|grep "Status of processor"|wc -l
On AIX run the command
lsdev -C|grep Process|wc -l
On HP-UX run the following commands (requires superuser privileges):
P=`echo processor_count/D | adb -k /stand/vmunix /dev/mem |tail -1|awk '{print $2}'` echo "The number of processors on `hostname` = $P"
On Tru64 run the command
/usr/sbin/psrinfo -v|grep "Status of processor"|wc -l
How about
cat /proc/cpuinfo | grep -i 'processor' | wc -l
Or even
dmesg | grep -i cpu
Look out for "Brought up x processors" in the last one
I don't know if it applies to Unix as well, but for Linux, from command line, see here: http://www.howtogeek.com/howto/ubuntu/display-number-of-processors-on-linux/

Kill respawing server running on port 3000 created by `node . > /dev/null 2> /dev/null < /dev/null &`

I'm using AWS CodeDeploy in which server running on pm2 dose not work due to explanation given here in troubleShoot documentation.
I followed the documentation and in AfterInstall script used node . > /dev/null 2> /dev/null < /dev/null & to run the node server in the background.
I've tried following ways to kill the server
fuser -k 3000/tcp
lsof -P | grep ':3000' | awk '{print $2}' | xargs kill -9
kill -9 $(lsof -t -i:3000)
but each time a new process respwans with a different PID.
How can I kill this background process and add it to the ApplicationStop script for CodeDeploy?
One of the problems with finding a pid with grep is that the grep pid will also show up as a result and can kill itself before the target, so try;
ps ax | grep node | grep -v grep
if it looks reasonable, review this;
ps ax | grep node | grep -v grep | awk '{print $1}'
then run the kill;
ps ax | grep node | grep -v grep | awk '{print $1}' | xargs kill -9
pkill is a less flexible option (no regex filtering) but if you use that be sure to use the -I flag so you don't kill anything you did not intend to.
I was able to kill using pkill node command.

Resources