29

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?

0

7 Answers 7

19

On OS X Lion with Homebrew:

$ brew install proctools

This downloads, builds and installs pgrep, pkill and pfind.

1
  • Would there be possible to build a GUI to work in the way xkill does but for OSX Commented Feb 26, 2015 at 12:08
16

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

2
  • 2
    Unfortunately as of 10.15.7, there is a nasty bug where pgrep and pkill when typically used with the -f option (match against the full argument list) silently truncate process lists to 2048 characters. Below the actual process list limit ARG_MAX 256 kB in syslimits.h (or from getconf ARG_MAX). ps won't truncate the list so you might be better off relying on that or proctools if you need to match on long process lists (such as java commands that have long classpath arguments).
    – tmoschou
    Commented Jan 19, 2021 at 23:35
  • 1
    @tmoschou You can find the source for Apple's pkill on opensource.apple.com/source. It's together with other utilities in the collection adv_cmds. Maybe you can spot the bug.
    – cachius
    Commented Sep 8, 2022 at 17:29
12

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)
2
  • 3
    proctools is also available via Homebrew Commented Aug 13, 2011 at 1:12
  • 1
    @AndrewMarshall is right. For homebrew try brew install proctools
    – cwd
    Commented Mar 29, 2015 at 2:48
11

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)"
}
4
  • I'm on a Z Shell, although there should be no problems in adapting this.
    – Eimantas
    Commented Aug 11, 2010 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
    Commented Sep 2, 2010 at 20:25
  • I suggest you remove the function keyword, it's deprecated. See this post
    – SiegeX
    Commented Jan 12, 2011 at 23:47
  • You are awesome.
    – Bijan
    Commented Jan 18, 2018 at 20:01
2

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).

1
  • Proctools doesn't compile well with Snow Leopard
    – user6325
    Commented Apr 6, 2011 at 2:35
1

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
1

This was my solution for pkill:

#!/bin/sh

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .