Skip to main content

Process Commands

pkill command in Linux

Learn various ways of using the pkill command to terminate a process by its name.

The pkill command is used to kill processes in Linux, yes, that is a pretty generous description that's the only job it does (and does it pretty well).

The difference between kill and pkill command is that the kill command works only on process ID whereas the pkill command works only with the process name.

Let me show how you can use the pkill command. I've divided this tutorial into 2 parts:

  • The syntax and options you get with the pkill command
  • Practical examples of the pkill command

Let's start with the first one.

How to use the pkill command in Linux

Here's a simple syntax you need to follow to use the pkill command:

pkill [options] pattern

Here,

  • [options]: with available options, you can modify the default behaviour of the pkill command. For example, using the -u flag, you can kill user-specific processes.
  • pattern: Here, you specify the name or pattern used to kill the processes. This can be a process name, a regular expression, or other criteria.

Now, let's take a look at the available options with the pkill command:

Option Description
-u USER Only kill processes owned by the specified user(s). You can provide the username or the user ID.
-f Kill processes based on the entire command line, not just the process name. This allows you to kill processes that have specific arguments or parameters in their command line.
-x Only kill processes whose name exactly matches the provided pattern.
-n Only kill the most recently started process that matches the specified criteria. This is helpful when you have multiple processes with the same name and want to kill only the newest one.
-s SESSIONID Only kill processes belonging to the specified session ID(s).
-P PID Only kill processes whose parent process ID matches the specified ID(s). This allows you to kill child processes of a specific parent process.
-g GROUPID Only kill processes belonging to the specified process group ID(s). A process group is a collection of related processes that share the same process group ID.
-o Only kill the oldest (least recently started) process that matches the specified criteria. This is useful when you want to kill the process that has been running for the longest time.
-SIGNAL Specifies the signal to send to the processes you want to kill. By default, pkill sends the SIGTERM signal.

Practical examples of the pkill command

In this section, I will walk you through some practical examples of the pkill command so you can have a better idea of how it can be used in practical scenarios.

1. Kill process by its name

In my opinion, this has to be the most effective way you can kill a process as all you have to do is append its name.

To kill the process by its name, use the following command syntax:

pkill process-name

For example, if I want to kill the firefox, then I'll use the following command:

pkill firefox

2. kill the process of a different user

📋
To kill a process of a different user you need superuser privileges.

If you want to kill a process of a different user, then you can use the -u flag by appending the username and process name in the following manner:

sudo pkill -u <username> <process-name>

For example, here, I killed the active process htop of the user kabir using the following command:

sudo pkill -u kabir htop
kill process of a different user using the pkill command

3. Kill process based on the entire command line

Suppose you are running a script with multiple arguments and now you want to kill the script that you executed with specific arguments. In that case, you can use the pkill command with the -f flag:

pkill -f "process-name --arg1 value1 --arg2 value2"

For example, here, I'm running a script named my_script.sh with 3 arguments: --file input.txt, --output output.txt and --timeout 30. So here's what I'll use to kill it:

pkill -f "./my_script.sh --file input.txt --output output.txt --timeout 30"
kill process based on the entier command line usig the pkill command

4. Kill the exact process

When you use the pkill command to kill a process, it may kill the process starting with the same name. At least, that's what happened to me.

When I used pkill firefox, it killed the Firefox browser. Fine! But the actual process name was firefox-bin which means it killed the processes starting with the firefox string.

To kill a process by its exact name, you use the -x flag in the following manner:

pkill -x process-name

For example, if I want to kill the Firefox browser by its exact name, then, I'll use the following:

pkill -x firefox-bix

5. Use a different kill signal with the pkill command

By default, the pkill uses the SIGTERM signal and for most processes, you should stick to it as it is a polite way of terminating a process.

To use a different kill signal, you can use the following syntax:

pkill -<signal_number/signal_flag> process

Here are some popular kill signals you can use with the pkill command:

Signal Flag Signal Number Description
HUP 1 Hangup signal, used to reload or restart a process.
INT 2 Interrupt signal, used to interrupt a process (typically Ctrl+C).
QUIT 3 Quit signal, used to terminate a process and perform a core dump.
KILL 9 Kill signal, used to forcefully terminate a process without allowing cleanup.
TERM 15 Termination signal, used to terminate a process gracefully (default).
STOP 19 Stop signal, used to suspend a process's execution.
TSTP 20 Stop signal, used to stop or pause a process (typically Ctrl+Z).
CONT 18 Continue signal, used to resume a stopped or paused process.

For example, if I want to kill the Firefox browser using the KILL signal, I can do that in two ways. Either I can use the kill signal directly (what I prefer):

pkill -9 firefox-bin

Or I can specify the signal I want to use (KILL in my case) in the following manner:

pkill -KILL firefox-bin

That's it!

Here's how to kill the process on a specific port

If you want to kill process on a specific port, then you can refer to our detailed guide on how to kill process on a specific port:

Kill Process Running on a Specific Port in Linux
Want to kill the processes running on specific ports? No need to know the process ID or name. You can terminate a process based on the port number it is using.

I hope you will find this guide helpful. If you have any queries, feel free to leave us a comment.

Kabir Thapar