pfind, pgrep and pkill on Mac OSX

Having taken these commands for granted on stock linux, it’s always a little waste-of-time to be listing out processes bound to specific ports that I want to kill; and then killing that process by its PID.

For instance,

calvin$ lsof -i -P | grep -i "listen"
VLCStream 309 calvin 6u IPv4 0xffffff80127fc4e0 0t0 TCP *:54340 (LISTEN)
VLCStream 309 calvin 9u IPv6 0xffffff80127f9700 0t0 TCP *:54340 (LISTEN)
Dropbox 310 calvin 19u IPv4 0xffffff8016c31880 0t0 TCP *:17500 (LISTEN)
Dropbox 310 calvin 25u IPv4 0xffffff8016e23c20 0t0 TCP localhost:26164 (LISTEN)
iTunes 2447 calvin 27u IPv4 0xffffff8026554c20 0t0 TCP *:3689 (LISTEN)
iTunes 2447 calvin 28u IPv6 0xffffff80127f9ac0 0t0 TCP *:3689 (LISTEN)
GoogleTal 5650 calvin 25u IPv4 0xffffff801724d500 0t0 TCP localhost:63495 (LISTEN)
GoogleTal 5650 calvin 29u IPv4 0xffffff8020123de0 0t0 TCP localhost:63496 (LISTEN)
Skype 6659 calvin 49u IPv4 0xffffff80182744e0 0t0 TCP *:29945 (LISTEN)
python 6841 calvin 5u IPv4 0xffffff801724b160 0t0 TCP localhost:8000 (LISTEN)
python 6843 calvin 5u IPv4 0xffffff801724b160 0t0 TCP localhost:8000 (LISTEN)

tells me that I have these processes listening to specific ports as above.

Killing specific processes that I want gone will then be `kill -9 6841 6843` for example.

Mac OSX does not come with pfind, pgrep and pkill by default.  To add in these programs that we take for granted on stock linux, we install

calvin$ sudo port -v install proctools

x ./opt/local/bin/pfind
x ./opt/local/bin/pgrep
x ./opt/local/bin/pkill
---> Cleaning proctools
---> Removing work directory for proctools
---> Updating database of binaries: 100.0%
---> Scanning binaries for linking errors: 100.0%
---> No broken files found.

and we will now have all our time-saving commands pfind, pgrep and pkill; and this

calvin$ pkill -9 python

will now kill off my python processes by name, particularly when I am working on my python projects and I need to terminate the process and I accidentally hit Ctrl+Z instead of Ctrl+C.

Ctrl+Z does not actually terminate my running python development server as that is “undo”.  Ctrl+C is the keyboard shortcut that terminates the python process properly and frees up the port.

  • stefantalpalaru

    Ctrl+Z suspends the current process in most (all?) shells. You can bring the suspended process back in foreground with ‘fg’ or see a list of processes with ‘jobs’.

  • Noitidart

    Can you please verify what version of OS X you are speaking of? I am using OS X 10.10.1 and it has pgrep out of the box, is this not normal?