Linux killall Command for Beginners (8 Examples)

We've already discussed the kill command that you can use in case you want to terminate processes in Linux. However, there exists another command line utility that you can use for the same purpose: killall. In this tutorial, we will discuss the basics of this command using some easy to understand examples.

But before we do that, it's worth mentioning that all examples included in this tutorial have been tested on Ubuntu 16.04 LTS.

Linux killall command

The killall command lets you kill processes by name. Following is the tool's syntax:

killall [OPTIONS] name

And here's how the man page explains the tool:

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 following Q&A-styled examples should give you a better idea on how the killall command works.

Q1. How to use killall command?

The tool's basic usage is very easy - all you have to do is to pass the name of the process as argument to killall. For example, to kill the gthumb process that was running on my system, I used killall in the following way:

killall gthumb

Note that in case you aren't aware of the exact name of the process, you can use the ps command to fetch this information.

Q2. Is killall case-sensitive?

Yes, it is. It throws an error 'no process found' error if the user fails to write the process name in correct case. Following is an example:

Is killall case-sensitive

However, if you want, you can force killall to ignore case using the -I command line option.

Killall ignore case

Q3. How to make killall ask before terminating process?

Suppose you want the killall command to ask for user permissions before it kills a process, then you can use the -i command line option. This will make the killall operation interactive.

For example:

How to make killall ask before terminating process

Q4. How to choose the signal killall sends to process?

By default, it's the SIGTERM signal that killall sends to processes. However, you can send other signals as well. You can use the options -s, --signal, and -SIGNAL to send these other signals.

However, for this to work, you need to know what all signals the killall command can send. This you can do using the -l command line option.

killall -l

Following is the list the aforementioned command produced on our system:

How to choose the signal killall sends to process

Q5. How to prevent killall from complaining if specified process doesn't exist?

If the process whose name is passed to killall isn't running (doesn't exist), then the tool conveys this to the user in output.

prevent killall from complaining if specified process doesn't exist

However, if you don't want the tool to produce such output, then you can use the -q command line option.

killall -q command option

So you can see the killall command stayed quiet when -q was used.

Q6. How to kill all processes owned by a user?

If the requirement is to kill all processes that a specific user owns, then you can use the -u option provided by killall. Needless to say, the option requires you to specify the username for the user as its input.

killall -u [user-name]

For example:

killall -u himanshu

Q7. How to kill processes based on time?

Suppose you want to kill all processes that have now been running for more than 5 hours, then you can do this using the -o option in the following way:

killall -o 5h

Of course, you can specify process names if you don't want to kill all processes. Similarly, if you want to kill processes than are, say, less than 4 hours old, then you can do that using the -y command line option.

killall -y 4h

Here's how the man page explains these options:

-o, --older-than
              Match only processes that are older (started  before)  the  time
              specified.   The  time is specified as a float then a unit.  The
              units are  s,m,h,d,w,M,y  for  seconds,  minutes,  hours,  days,
              weeks, Months and years respectively.

 -y, --younger-than
              Match  only  processes that are younger (started after) the time
              specified.  The time is specified as a float then a  unit.   The
              units  are  s,m,h,d,w,M,y  for  seconds,  minutes,  hours, days,
              weeks, Months and years respectively.

Q8. How to make sure killall only returns after processes die?

After the signal is delivered by killall to the process, if you want to make sure the command only returns after the process dies, then you can do this using the -w command line option, which forces killall to wait.

killall -w [process-name]

Here's how the man page describes this option:

-w, --wait
              Wait  for  all killed processes to die.  killall checks once per
              second if any of the  killed  processes  still  exist  and  only
              returns if none are left.  Note that killall may wait forever if
              the signal was ignored, had no effect, or if the  process  stays
              in zombie state.

Conclusion

Killall may not be a cakewalk for newbies, but it isn't very complex as well. You need to practice the command line options it offers. We've discussed majority of them, so this tutorial should be a good starting point. Once you are done with these, you can read more about the command by heading to its man page.

Share this page:

Suggested articles

0 Comment(s)

Add comment