Macos List Ports For Process



Netstat output is going to vary significantly. For example, I ran your version right now and it does show all of the listen ports, but also around 100 ESTABLISHED connections. So if my goal is to list my open ports, filtering out everything else is simply a better solution. – Christopher Hunter May 19 '20 at 21:37. Hello, On Linux, I can use 'ps -efL grep processname' to list all threads that belong to a running process.L has a different meaning on AIX and I could not find an equivalent flag in the man pages. Lsof -i tcp:portnumber - will list the process running on that port. Kill -9 PID - will kill the process. In your case, it will be. Lsof -i tcp:3000 from your terminal find the PID of process.

Sometimes you just wanna kill an old process thats still listening on a port.

** TLDR: Here’s a one liner to find and kill a process on a given port (in the below example, port = 8080) if you already know what that process is **

Breakown of the commands

Macos list process using port

Get the process id or PID of the process running a port on your mac by running either of the following commands. Replace $PORT with the port you wish to examine.

Once you have the PID, you probably want to know the process corresponding to the PID, just in case you are the curious kind.

Macos List Ports For Processing

If you then need to kill that process

References

  • https://stackoverflow.com/questions/4421633/who-is-listening-on-a-given-tcp-port-on-mac-os-x
  • https://superuser.com/questions/632979/if-i-know-the-pid-number-of-a-process-how-can-i-get-its-name

Related Posts:

How to find ports that are already 'in use' on macOS:

This is helpful if you are trying to figure out which process is using a port so that you can kill that process — for example, if you have a web server running in the background but now want to re-use that port for a different server.

Output of this command looks as follows:

This shows the results for a node http-server listening on port 8080. One could then kill this server using the PID shown.

Explanation

lsof is a program to 'list open files' and variants exist for major UNIX dialects such as macOS and Linux.

  • sudo: you need sudo privileges to determine which files are open by other users on your system (and ports are just open files in a large sense)
  • -P: 'inhibits the conversion of port numbers to port names for network files'. Port numbers can be mapped to names for commonly used services, such as http for port 80. If you are trying to find which which process is running on a port number, it makes it more difficult if you need to think through the names as well. You can see which names are mapped on your system by viewing the /etc/services files.
  • -i TCP: filter the listing of files to internet addresses matching the argument. The argument here is just TCP but more general internet addresses of the form [46][protocol][@hostname|hostaddr][:service|port] are accepted as well. For example, if you are looking for processes using port 8080, you could provide that here, such as -i TCP:8080.
  • -s TCP:LISTEN: used together with -i TCP, causes only network files with TCP state LISTEN to be listed.
Macos list ports for processing

Macos List Ports For Processes

ListMacos List Ports For Process

The command output varies significantly based on the type of file, but here, as we have filtered to only TCP network files with a LISTEN state, the output can be understood as follows:

  • command: partial command name
  • PID: process ID
  • NAME: the local host name or IP number followed by a colon, the port, and the two-part remote address (if applicable)

Source: man lsof

Please enable JavaScript to view comments.



Comments are closed.