To view a process that uses a port
The simplest commands are:
-i :端口号
If you want to use Administrator privileges then it is:
sudo lsof -i :端口号
So looking at a process using a port number 3000 can be used:
lsof -i :3000
You can also use:
sudo lsof -i :3000
The results of the terminal are as follows:
HarveydeMac-mini:~ harvey$ sudo lsof -i :3000COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEnode 20771 harvey 17u IPv4 0x56e527dafba04d5 0t0 TCP *:hbci (LISTEN)
COMMAND
Represents the name of the process using the port, which is clearly the 3000 port used by node.
PID
More critical, representing the process number that uses the port.
(LISTEN)
Indicates that the process is in a listening state, that is, the process is active.
sudo lsof -i tcp:3000
The execution result is the same as the previous command.
There are also some optional parameters on the above command, which are then derived in addition to a scoop command. It can also be used to check the process of using a port.
Like what:
sudo lsof -i :3000 | grep LISTEN
Execution Result:
HarveydeMac-mini:~ harvey$ sudo lsof -i :3000 | grep LISTENnode 20771 harvey 17u IPv4 0x56e527dafba04d5 0t0 TCP *:hbci (LISTEN)
And:
sudo lsof -i tcp:3000 | grep LISTEN
The execution result is the same as the previous command.
command to close a process
To close a process that uses a port:
sudo kill -9 PID
For example, I turned off the node process using port 3000
sudo kill -9 20771
Of course, we can also disable the process by not applying administrator privileges
kill -9 20771
To view a process shutdown port using a port