How to continuously monitor PIDs connecting to local sockets

networkingprocesssocketss

Some local processes are connecting and disconnecting to 127.0.0.1 on port 1234.

I would like to log all connection to this port (or to server process).

I've tried

 ss -tpn | grep 1234 

It shows a list, but then stops; It doesn't keep logging new connections, so I can't catch the sockets nor PID of owner.

What tool can I use to discover PID of the connecting and disconnecting sockets?

Best Answer

There are two tools you could use to monitor TCP connect events on Linux:

The difference between the two is that the former provides options for customizing output (e.g., filtering by PID or port number) while the latter is a more simplistic tool and doesn't provide fancy options.

For your use case, the most simple option would be to install bcc and run:

tcpconnect.py -P 1234

If you install these tools using your distro's package manager, keep in mind that some distros don't place tcpconnect in /usr/bin and place them under somewhere else like /usr/share instead. So be sure to check where your distro places these files if you can't find them.

Related Question