Tags

Linux killall command explained with examples

There are times when you would want to kill a process due to the
any reason like its high CPU usage, high memory usage or simply you
believe that its a virus or a malware. There are various
commands/utilities available in Linux for doing this. One such command
is 'killall'. Lets discuss the 'killall' utility in this article
through some examples.

Before jumping on to the examples, lets briefly take a look at it s
description from the man page :

DESCRIPTION :
killall sends a signal to all processes running any of the specified
commands. If no signal name is specified, SIGTERM is sent. Signals can
be specified either by name (e.g. -HUP or -SIGHUP ) or by number (e.g.
-1) or by option -s. If the command name is not regular expression
(option -r) and contains a slash (/), processes executing that
particular file will be selected for killing, independent of their
name. killall returns a zero return code if at least one process has
been killed for each listed command, or no commands were listed and at
least one process matched the -u and -Z search criteria. killall
returns non-zero otherwise. A killall process never kills itself (but
may kill other killall processes).

The output above confirms that the 'testKill' process was indeed
killed by the 'killall' command.

2. Direct the 'killall' command to ignore case

The 'killall' command is case sensitive. This means that you have
to carefully provide the case of executable name when killing it using
'killall'. Following example confirms it :

$ killall TestKillTestKill: no process found

So I changed the case of a letter in the executable name and tried
to kill the corresponding process using the 'killall' command and I got
the result above which clearly said that killall could not find any
such process.
To avoid such problems, there exists a flag '-I' that can be used. If
-I is used then killall does not check the case. Lets confirm it using
the following example :

$ killall -I TestKill[1]+ Terminated ./testKill

So we see that the flag -I indeed works!!!!

3. Sending different signals to kill processes

Ever wondered how killall kills the processes? Well, it sends the
signal SIGTERM to the process to kill it. But there could arise some
scenarios where the user wants killall to kill a particular process but
the process is ignoring the SIGTERM signal and hence does not get
killed. Well, in these cases the flag '-s' comes to rescue. Through
this flag we can send any of the available signal to the process
through 'killall' command. Lets see the example below :

$ killall -s SIGINT testKill[2]- Interrupt ./testKill

So we see that through the -s flag, a signal SIGINT was sent to
the process.
Now one would ask that where to get the list of available signals?
Well, for this too the killall command provides a flag '-l'.

4. Killing multiple processes interactively

Now, there could be a case where one would like to see the command
to interact with the user before killing individual process. To cater
this requirement, there is a flag '-i' that turns the interactive mode
on.

So, as we can see in the above output the killall command asked
for user approval before killing individual process. Also while running
this command, since I was in a different directory (than which contains
the executable or from where I ran the executable) so the output also
showed the working directory of the process and working directory from
where the killall command was issued.

5. Do the tasks quietly

What if a user doest not want the extra information that killall
command spits out in some cases like when no process matched the
process name given to killall? Well, for this purpose, there exists a
flag '-q' that instructs killall command to keep quite wherever
possible. For example :

$ killall testKill testKillalltestKill: no process foundtestKillall: no process found

$ killall -q testKill testKillall$

So we see that initially we tried to kill two non-running
processes and got "no process found" information in output but later
when -q flag was used then no such information was thrown out by the
killall command.

An interesting observation

For an option '-e', the man page of this command mentions that :

-e, --exact Require an exact match for very long names. If a
command name is longer than 15 characters, the full name may be
unavailable (i.e. it is swapped out). In this case, killall will kill
everything that matches within the first 15 characters. With -e, such
entries are skipped. killall prints a message for each skipped entry if
-v is specified in addition to -e,

So to practically test the above statement, I created two
processes/executable with names 'abcdefghijklmnopq' and 'abcd'. We can
see that the length of the name of first process/executable is 17
characters (which is greater than 15) while that of the second is 4
(which is less than 15). Also the name of the second process/executable
matches the first 4 characters of the name of first process/executable.
Now when I executed the two binaries, I could see the following output
in the ps command :

As can be seen in above output, 'abcd' was still running. But If
we go by the man page then this could have only happened if we would
have used the '-e' flag.
If anybody can throw some light on this issue, then he/she is welcome
to start a discussion here.