When you want to know which port was already in use you can use netstat -apn | grep 27777
. The result is below:
> tcp 0 0 ::ffff:43.69.96.68:27777 :::* LISTEN 79339/java
Then you can find which process use the PID 79339.
ps -aux | grep 79339
Now I wonder if there is a command to find which process uses the port that was already in use.
I tried ps -aux | grep netstat -apn | grep 18888 | awk -F '[ /]+' '{print $7}'
but it is no working.
First run netstat
and print out pid like you almost did. Then run ps
and grep
pid, using "word" option to avoid grepping parts of the digits (if pid
is 456
you don't want to match 14567
)
Put that in a bash script and you're done.
pid=$(netstat -apn | awk -F '[ /]+' '{print $7}')
ps -aux | grep -w $pid