how to kill a service based on port number on mac 1

how to kill a service based on port number on mac

lsof -P | grep ':PortNumber' | awk '{print $2}' | xargs kill -9

Here is what the above code is Doing:
1. lsof -P | grep ‘:PortNumber’
– lsof is a command that lists all open files.
– -P is a flag that tells lsof to not resolve port numbers to port names.
– grep is a command that searches for a pattern in a file.
– ‘:PortNumber’ is the pattern we’re searching for.
– This command will return a list of all processes that have a port open on PortNumber.
2. awk ‘{print $2}’
– awk is a command that searches for patterns in a file and performs actions on them.
– ‘{print $2}’ is the action we’re performing.
– This command will print the second column of the output of the previous command.
3. xargs kill -9
– xargs is a command that executes a command on a list of arguments.
– kill -9 is the command we’re executing.
– This command will kill all processes that have a port open on PortNumber.

Similar Posts