1. Overview

In Linux, managing processes efficiently is a fundamental task for system administrators and users alike. Processes are running instances of programs on a system, and sometimes it’s necessary to terminate or manage them. Traditionally, we can use the kill command to terminate processes by specifying its process ID (PID).

However, this requires looking up the PID for each process, which can be cumbersome. Effectively, this is where the killall command comes in handy, as it enables users to terminate processes by name, simplifying the process management workflow.

In this tutorial, we’ll explain how to use the killall command on Linux systems as we explore different options that best fit our needs.

2. Comparing kill With killall

Let’s first shed some light on the differences between the kill and killall commands.

2.1. Choosing the Right Termination Tool

Terminating processes on Linux is a common task, but there are two primary commands with distinct approaches: kill and killall. Throughout this section, we’ll explore the key differences between these commands to help us choose the most effective tool for our needs based on different aspects, such as flexibility, usability, and syntax.

Let’s understand the differences regarding flexibility:

  • kill provides more control over how we terminate a process, as we can specify a signal to be sent, such as SIGTERM for a graceful termination or SIGKILL for immediate termination
  • killall offers limited flexibility, however, it may offer some options to control signal behavior, as it typically defaults to sending SIGTERM, providing less granular control over termination

The kill command offers greater precision as it targets a specific process identified by its unique PID. This is ideal when we need to terminate a single instance of a program that may have multiple processes running.

On the other hand, killall is faster and more convenient for common tasks. Accordingly, we don’t need to know the exact PID beforehand, making it suitable for quickly stopping all program instances.

Finally, although both options satisfy the same need, the syntax is slightly different:

  • kill requires the PID of the specific process we want to terminate and offers the option to target multiple processes by listing their PIDs separated by spaces
  • killall leverages the process name to find and terminate all matching processes running on the system

We’ll elaborate more on the syntax part within the upcoming sections with practical examples.

2.2. Criteria of Differences

Let’s look at a quick summary of the differences between these two commands:

Feature kill killall
Flexibility More control over signals Limited signal options
Usability Precise, needs PID Faster, uses process name
Syntax kill [signal] PID… killall [options] process_name

In other words, we can list the benefits of the killall command in a few points:

  • name-based termination: with killall, we can specify processes to terminate by their names rather than their PIDs, which makes the command more intuitive and easier to use
  • simplified workflow: by directly specifying process names, killall streamlines process management
  • multiple process termination: killall can terminate all processes that match a given name, reducing the need for multiple commands

Hence, killall offers several advantages over using kill with PIDs.

2.3. Syntax Comparison

As theoretically explained before, the differences in syntax between the two commands aren’t drastic. However, opting for the better option greatly relates to our use case. In this section, we’ll explain when to utilize either option.

Let’s kick it off by understanding the syntax of the kill command:

# pid=$(pgrep myprocess)
# kill $pid

As shown in the snippet above, the kill command requires the PID of the specific process we want to terminate. Moreover, we can target multiple processes by listing their PIDs separated by spaces.

In the first line, with the help of the pgrep command, we initialized a variable called pid that holds the value of the process named myprocess. Afterward, we passed this variable as an argument to the kill command to kill this process.

On the other hand, let’s check the syntax of the killall command:

killall [options] process_name

Here, we’re deleting all instances of the myprocess process name simply without using any options:

# killall myprocess

Accordingly, killall is faster and more convenient, especially when dealing with multiple instances of the same process. In particular, we utilize this command when our goal is to directly terminate all instances by name without needing to know the PIDs. Importantly, it leverages the process name to find and terminate all matching processes running on the system.

3. Exploring killall Options

Any Linux administrator or user needs to leverage the options available with any command to tailor his objective to his specific needs. So, let’s delve into the various options available with killall and how they can be used effectively.

3.1. Choosing the Right Signal

By default, the killall command sends a SIGTERM signal to terminate processes. However, we can specify a different signal using the -s or –signal option:

$ killall -s SIGKILL firefox

For instance, this command sends a SIGKILL signal to terminate all the firefox process instances immediately.

3.2. Users and Case Sensitivity

As we know, Linux is case-sensitive. Therefore, a useful option that can enable us to avoid the hassle of not abiding by the case sensitivity restrictions imposed by Linux is the -I option. For example, the following two commands are equivalent:

$ killall -I chromium
$ killall -I Chromium

By running either of these commands, we’ll successfully kill all the processes named chromium – including those with any combination of cases for any of the letters, as long as the letters in the process name appear in the same order. Furthermore, we used -I for simplicity, but we can also use –ignore-case.

Next, we can terminate processes owned by a specific user using the -u or –user option:

$ killall -u john firefox

Here, we terminate all firefox processes owned by the user john.

3.3. Regular Expressions

In addition, we can use regular expressions to specify process names for termination using the -r or –regexp option:

$ killall -r ^webserver.*

The -r flag in killall signifies that the provided process name pattern should be interpreted as a POSIX extended regular expression. Particularly, this allows for more complex matching of process names when terminating processes.

The snippet above would terminate all processes whose names start with “webserver” followed by any characters (represented by the .* part). In essence, this could match processes like “webserver, webserver1, or “webserver_main“, for instance.

3.4. Security Context

Last but not least, the -Z flag, also known as –context, allows us to target processes based on their security context. In Linux systems with Security-Enhanced Linux (SELinux) enabled, processes are assigned security labels that define their access rights and capabilities.

With -Z, we can specify a security context pattern as a POSIX extended regular expression. The killall command will then search for processes whose security labels match the provided pattern. Moreover, we can optionally include a process name further to refine the termination within the matching security context:

$ killall -Z webserver_t httpd

This command would terminate only httpd processes whose security label starts with webserver_t. Due to the potential for wider matching, it’s recommended to use -Z cautiously and only when simpler process identification methods are insufficient.

Finally, since we explored a wide variety of the options or flags that we can utilize with the killall command, let’s refine our environment utilizing two or more options together:

$ killall -s SIGTERM -u john firefox

Here, we send a SIGTERM signal to terminate all firefox processes owned by the user john.

4. Conclusion

In this tutorial, we learned that the killall command is a versatile tool for terminating processes based on their names, offering flexibility and ease of use compared to using process IDs directly with kill.

By mastering killall and its various options, we can efficiently and more effectively manage processes on our Linux system.

Notify of
Inline Feedbacks
View all comments