The pgrep
and pkill
utilities were introduced in Sun's Solaris 7 and, as g33klord noted, they take a pattern as argument which is matched against the names of running processes. While pgrep
merely prints a list of matching processes, pkill
will send the specified signal (or SIGTERM
by default) to the processes. The common options and semantics between pgrep
and pkill
comes in handy when you want to be careful and first review the list matching processes with pgrep
, then proceed to kill them with pkill
. pgrep
and pkill
are provided by the the procps package, which also provides other /proc
file system utilities, such as ps
, top
, free
, uptime
among others.
The killall
command is provided by the psmisc package, and differs from pkill
in that, by default, it matches the argument name exactly (up to the first 15 characters) when determining the processes signals will be sent to. The -e
, --exact
option can be specified to also require exact matches for names longer than 15 characters. This makes killall
somewhat safer to use compared to pkill
. If the specified argument contains slash (/
) characters, the argument is interpreted as a file name and processes running that particular file will be selected as signal recipients. killall
also supports regular expression matching of process names, via the -r
, --regexp
option.
There are other differences as well. The killall
command for instance has options for matching processes by age (-o
, --older-than
and -y
, --younger-than
), while pkill
can be told to only kill processes on a specific terminal (via the -t
option). Clearly then, the two commands have specific niches.
Note that the killall
command on systems descendant from Unix System V (notably Sun's Solaris, IBM's AIX and HP's HP-UX) kills all processes killable by a particular user, effectively shutting down the system if run by root.
The Linux psmisc utilities have been ported to BSD (and in extension Mac OS X), hence killall
there follows the "kill processes by name" semantics.
when trying to find the PID of firefox
, you launch a new process the filters the all the unwanted processes. this filter process (grep firefox
) also contains the search-term "firefox" and thus finds itself.
whenever you restart ps ax | grep firefox
you launch a new grep
-process, hence it's PID keeps changing.
So, the short answer is:
use PID 2213 to kill firefox
If you want to get rid of the false positive, you can use another grep
to filter it out:
$ ps ax | grep firefox | grep -v grep
yet another option is to use pgrep
(which will only give you the PID of the found processes)
$ pgrep firefox
2213
Best Answer
Is this on Linux?
There are actually a few subtly different versions of the command name that are used by
ps
,killall
, etc.The two main variants are: 1) the long command name, which is what you get when you run
ps u
; and 2) the short command name, which is what you get when you runps
without any flags.Probably the biggest difference happens if your program is a shell script or anything that requires an interpreter, e.g. Python, Java, etc.
Here's a really trivial script that demonstrates the difference. I called it
mycat
:After running it, here's the two different types of
ps
.Firstly, without
u
:Secondly, with
u
:Note how the second version starts with
/bin/sh
?Now, as far as I can tell,
killall
actually reads/proc/<pid>/stat
, and grabs the second word in between the parens as the command name, so that's really what you need to be specifying when you runkillall
. Logically, that should be the same as whatps
without theu
flag says, but it would be a good idea to check.Things to check:
cat /proc/<pid>/stat
say the command name is?ps -e | grep db2
say the command name is?ps -e | grep db2
andps au | grep db2
show the same command name?Notes
If you're using other ps flags too, then you might find it simpler to use
ps -o comm
to see the short name andps -o cmd
to see the long name.You also might find
pkill
a better alternative. In particular,pkill -f
tries to match using the full command name, i.e. the command name as printed byps u
orps -o cmd
.