Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: May 28, 2024
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.
Let’s first shed some light on the differences between the kill and killall commands.
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:
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:
We’ll elaborate more on the syntax part within the upcoming sections with practical examples.
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:
Hence, killall offers several advantages over using kill with PIDs.
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.
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.
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.
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.
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.
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.
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.