What is the difference between kill , pkill and killall?
Solution 1
The kill
command is a very simple wrapper to the kill
system call, which knows only about process IDs (PIDs). pkill
and killall
are also wrappers to the kill
system call, (actually, to the libc library which directly invokes the system call), but can determine the PIDs for you, based on things like, process name, owner of the process, session id, etc.
How pkill
and killall
work can be seen using ltrace
or strace
on them. On Linux, they both read through the /proc
filesystem, and for each pid (directory) found, traverses the path in a way to identify a process by its name or other attributes. How this is done is technically speaking, kernel and system specific. In general, they read from /proc/<PID>/stat
which contains the command name as the 2nd field. For pkill -f
and pgrep
examine the /cmdline
entry for each PID's proc entry.
pkill
and pgrep
use the readproc
system call, whereas killall
does not. I couldn't say if there's a performance difference: you'll have to benchmark that on your own.
Solution 2
kill and killall are tools which provide a way to kill a process. The first by its PID, the second by its name. pgrep (list) and pkill (kill by default) are tools which provide a way to send message to a process by its name or other attributes see: http://linux.die.net/man/1/pkill For more info about signals: http://linux.die.net/man/7/signal
Related videos on Youtube
Ijaz Ahmad
Linux Bash Python Kubernetes ( On-prem, AWS, GKE, DO ) Docker ElasticSearch Gitlab/Github Jenkins DevOps/SRE/Automation/CI/CD Infra as Code, Pulumi, Terraform
Updated on September 18, 2022Comments
-
Ijaz Ahmad over 1 year
I am familiar with
kill
command , and most of the time we just usekill -9
to kill a process forcefully, there are many other signals that can be used withkill
. But I wonder what are the use cases ofpkill
andkillall
, if there is already a kill command.Do
pkill
andkillall
use thekill
command in their implementation? I mean they are just wrappers overkill
or they have their own implementation?I would also like to know how
pgrep
command gets the process id from the process name.Do all these commands use the same underlying system calls? Is there any difference from a performance point of view, which one is faster?
-
Jpark822 over 8 yearsTwo things: Why use
kill -9
by default? -15 (please stop) and -1 (modem has hung up, please CLEANLY closed yourself) are much more polite. Secondly. Beware of using killall on non-linux boxes. It might behave differently. (E.g. on solaris it kills all. NOT FILTERED on process names). -
Byte Commander almost 5 years
-
-
Ijaz Ahmad over 8 yearsDo you mean
killall
is used to kill a process by its name? and it uses pgrep for this purpose? andkillall
also kills all the child processes ,? what signalkillall
uses by default? -
Jenny D over 8 years...unless you're on Solaris, in which case
killall
will kill all processes you have the right to kill, so that if you're root, you're effectively rebooting the server. -
dervishe over 8 yearsyep:
killall chromium
will kill the chromium process,pgrep chromium
will give you the PID list,pkill chromium
will kill chromium. killall will send by default SIGTERM signal (as pkill)