Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Are there alternatives to pgrep and pkill commands on Mac OS X or should I just create aliases for them using other commands available for me?

share|improve this question

8 Answers 8

up vote 5 down vote accepted

following should help for pgrep

poor-mans-pgrep-on-mac-os-x

share|improve this answer
    
This will work. Thanks! –  Eimantas Aug 11 '10 at 5:03

Assuming that you are using some relatively recent version of Bash in the Mac, you could write your own version of pgrep as function and then add that to your .bashrc file:

function pgrep() {
    ps aux | grep $1 | grep -v grep
}

as for pkill you can use the following:

function pkill() {
    local pid
    pid=$(ps ax | grep $1 | grep -v grep | awk '{ print $1 }')
    kill -9 $pid
    echo -n "Killed $1 (process $pid)"
}
share|improve this answer
    
I'm on a Z Shell, although there should be no problems in adapting this. –  Eimantas Aug 11 '10 at 5:14
1  
or /bin/kill $(ps ax | awk '$5 ~ /'"$1"'/ { print $1 }') for a more faithful pgrep (process name only, not args. I use /bin/kill out of habit because it reliably takes more than one PID to kill. There are other tricks, if you're in control of the regexp to never have to 'grep -v grep' - that way you can pkill greps!) –  jrg Sep 2 '10 at 20:25
    
I suggest you remove the function keyword, it's deprecated. See this post –  SiegeX Jan 12 '11 at 23:47

Proctools includes pgrep and pkill and is available for OpenBSD and OSX. It hasn't been updated in a while, but it should still work (at least on OSX which rarely modifies its ABI).

share|improve this answer
    
Proctools doesn't compile well with Snow Leopard –  user6325 Apr 6 '11 at 2:35

You could use MacPorts: sudo port install proctools

Here's the result of port search pgrep:

proctools @0.4pre1 (sysutils)
    pgrep, pkill and pfind for OpenBSD and Darwin (Mac OS X)
share|improve this answer
2  
proctools is also available via Homebrew –  Andrew Marshall Aug 13 '11 at 1:12

On OS X Lion with Homebrew:

$ brew install proctools
$ sudo brew link proctools

This downloads, builds and installs pgrep.

share|improve this answer
    
+1. This should be the accepted answer, really. –  Henrik Jan 8 '12 at 3:58

you could try killall. It kills processes by name. Any processes that match the string you pass in are killed.

killall httpd ( kill all apache processes )
killall php ( kill all php process )

If you do

killall -s man ( kill any manual page processes, if a user is using man [command]

it will show you a list of processes that would be killed like below.

kill -TERM 70836

If you want a different signal do the following

killall -9 processname
share|improve this answer

This was my solution for pkill:

#!/bin/sh

for X in `ps acx | grep -i $1 | awk {'print $1'}`; do
    kill -9 $X;
done
share|improve this answer

You don't need an alternative anymore: since MacOS 10.8 pgrep and pkill are available per default.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.