1. Create script
Sometimes we need to make sure, that some particular daemon is running on some port, and for that reasons, we can use a simple script:
lsof -nP -i4TCP:$PORT
Don’t forget to replace $PORT with your custom value, like 80.
Let’s see how it works.
I have Nginx up and running on port 80, so I can check that like this:
lsof -nP -i4TCP:80
And the output will be like this:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 68074 user 10u IPv4 0x7e97088702084797 0t0 TCP *:80 (LISTEN)
nginx 68077 user 10u IPv4 0x7e97088702084797 0t0 TCP *:80 (LISTEN)
nginx 68078 user 10u IPv4 0x7e97088702084797 0t0 TCP *:80 (LISTEN)
nginx 68079 user 10u IPv4 0x7e97088702084797 0t0 TCP *:80 (LISTEN)
nginx 68080 user 10u IPv4 0x7e97088702084797 0t0 TCP *:80 (LISTEN)
nginx 68081 user 10u IPv4 0x7e97088702084797 0t0 TCP *:80 (LISTEN)
nginx 68082 user 10u IPv4 0x7e97088702084797 0t0 TCP *:80 (LISTEN)
nginx 68083 user 10u IPv4 0x7e97088702084797 0t0 TCP *:80 (LISTEN)
nginx 68084 user 10u IPv4 0x7e97088702084797 0t0 TCP *:80 (LISTEN)
2. Add function to ~/.bash_profile
2.1. Open bash profile:
nano ~/.bash_profile
2.2. At the end of the file add:
wholisten_tcp() {
lsof -nP -i4TCP:$1;
}
2.3. Press “Ctrl-O” to save changes.
2.4. Reload .bash_profile:
source ~/.bash_profile
2.5. To use it, now you can just call:
wholisten_tcp 80
Enjoy )).