Mastering the Linux pkill Command: A Beginner’s Guide with 5 Practical Examples

In Linux, if you need to terminate a process from the command line, the kill command can be used by providing the process ID (PID) of the process you want to stop. However, did you know there’s also a way to kill processes without needing to specify their PIDs?

Indeed, there’s a utility named pkill that allows for this. In this article, we’ll explore the fundamentals of the pkill command using straightforward examples. It’s important to note that all examples here have been tested on machines running Ubuntu 18.04 LTS and Debian 12.

Understanding the Linux pkill Command

The pkill command in Linux offers a simplified method to terminate processes. The command syntax is as follows:

pkill [options] pattern

According to its manual, pkill:

pkill - signal processes based on name and other attributes

Here are some examples presented in a Q&A style to help deepen your understanding of how the pkill command functions.

Q1. How to Use the pkill Command?

Basic usage is quite simple. Just execute ‘pkill’ with the process name as the input.

For instance, if you want to terminate the ‘gedit’ process, you can use pkill in the following manner:

pkill gedit

That’s all you need to do. If the gedit process is running, it will be terminated upon execution of this command.

Q2. How to Send a Different Signal Using pkill?

The pkill command sends a signal to the target process, typically the SIGTERM signal by default. If required, you can change the signal using the –signal command-line option.

For example:

pkill --signal SIGKILL gedit

Q3. How to Terminate Processes Based on the Full Command Line?

By default, the provided name (pattern) is matched solely against the process name. However, you can command pkill to match the entire command line using the -f option.

For example, assume two ping commands are active on your system. Here’s a snippet from the ps command output:

...
root 19253 0.0 0.1 9180 2916 pts/1 S+ 19:24 0:00 ping google.com
root 19331 0.0 0.1 9180 2912 pts/2 S+ 19:25 0:00 ping howtoforge.com
...

If the goal is to only terminate the ‘ping google.com’ command using pkill, you might initially try:

pkill ping

This would end both processes. To avoid this, use the -f option as shown:

pkill -f "ping google.com"

Command execution in the first terminal:

Process killed with pkill

Result in the second terminal where the ping process was running:

Process in second terminal stopped with pkill command

Upon executing the specified command, only the input command was terminated.

Q4. How to Make pkill Command Case Insensitive?

By default, pkill is case sensitive, differentiating between uppercase and lowercase letters. To make pkill case insensitive, utilize the -i option.

pkill -i [process-name]

Q5. How to Refresh the syslog Configuration File Using pkill?

To cause syslog to reread its configuration file, use the pkill command like this:

pkill -HUP syslogd

Conclusion

The pkill command is an invaluable tool for process termination. We’ve covered several of its command-line options. After practicing these, further exploration of the command is recommended through its detailed man page.

Frequently Asked Questions (FAQ)

What is the difference between pkill and kill?

While both are used to terminate processes, pkill can identify a process by name, allowing for greater flexibility and ease when the process ID is unknown. kill requires explicit process ID input.

Can pkill affect more than one process at a time?

Yes, if multiple processes match the criteria specified in pkill, all of those processes will be terminated.

Is it safe to use pkill for critical system processes?

Exercise caution when using pkill for critical processes. Terminating essential processes can lead to system instability or data loss.

How can I see which processes will be killed by pkill?

You can use the -n or --newest option to preview the most recent process that would be killed without actually terminating it.

Does pkill require superuser privileges?

pkill may require superuser privileges depending on the process you attempt to terminate. For system-level processes, you might need to prepend the command with sudo.