Mutual query between Linux port number and PID
Recently I used Linux to play with Tomcat. When I started it, I always reported an error (8080/8009/8005). So I sorted out the PID and port commands on the Internet for record.
1. query the PID Number by the port number
First, when myeclipse reports an error, it will prompt: "port 8009 is occupied", so you have to check which processes are running under this port based on this port
Run the following command to view all ports running on the current system: sudo netstat-antup
Note: Check the sudo permission. Otherwise, some PIDs cannot be viewed. I used to go around a lot because I didn't use the root permission.
2. query the port number with PID
If you know the process PID, how can you view the process status of the PID?
Run the command: sudo netstat-antup | grep PID to query the opening status of the target port! The premise is that you need to know the PID Number:
Is to use PID 1024 to view the port, we can see that the port is in the LISTEN listening status, and the port is 80.
3. query the PID number with the process name
Sometimes we are familiar with some common processes, so we can use the ps command to query the target PID:
Run the command: sudo ps-ef | grep process name to view the PID of the process. But the premise is that you need to know the name of the process, for example, some background processes cannot be viewed.
Use this command to check that the PID of the mysql database is 2807 and the port number is 3306.