pkill Linux Command | Explained

In Linux, the “pkill” command kills or terminates a running process based on its name. This command sends a specified signal, i.e., SIGTERM, to the utility to end the current process from the environment. The purpose of this guide is to explain the “pkill” command with practical implementations in Linux. The content that supports this article is enlisted below:

  • How Does the pkill Command Work in Linux?
  • How to Use the pkill Command in Linux?
    • Example 1: Kill a Specific Process Using Name
    • Example 2: Kill All Processes of Active User
    • Example 3: HangUp the Ping Process Using pkill
    • Example 4: Kill Process With Full Argument
    • Example 5: Kill Running Processes of Multiple Users
    • Example 6: Kill Gnome Process of Specific User
    • Example 7: Kill Most Recent Process

How Does the pkill Command Work in Linux?

The “pkill” command is quite useful for stopping a specified process in a hundred processes. The syntax of “pkill” commands with arguments is as follows:

Syntax:

pkill [options] <pattern>

In the above syntax, the “pkill” command acquires “options” that execute processes based on flags, and “pattern” specifies the regular expression. Different options and their descriptions are explained below:

OptionsDescription
-oIt means to kill the oldest processes.
-nThe options kill the newest processes.
-xThis option kills processes having the same pattern.
-uIt refers to killing processes that are owned by other users.
-signalIt sends a signal to the process.

An alternative way is by sending a signal. Each signal has its functionalities, the three most common signals are enlisted below:

SignalDescription
1 (HUP)This signal specifies reloading the process.
9 (KILL)It represents killing the process.
15 (TERM)The signal completely stops the process.

To explore more options, you can utilize the “pkill –help” command in the terminal below:

$ pkill --help

How to Use the pkill Command in Linux? 

This section enlists various examples to demonstrate the practical usage of skill Commands in Linux.

Example 1: Kill a Specific Process Using Name

An example is considered in which an existing process, “firefox”, is already running on our system. To stop this process, the “pkill” command is utilized in the below steps:

Get the Process Name to be Killed:

To display the list of all the active processes is possible through the “top” command as below:

$ top

Terminate the Process:

To terminate the existing process, specify the name of the process as “firefox” as below. Additionally, the value “-15” helps to stop the process without any environmental issues:

$ pkill -15 firefox

Verify Terminated Process:

Now, you can verify that the “firefox” process has been successfully terminated from the active processes:

Let’s explore another aspect of the “pkill” command.

Example 2: Kill All Processes of Active User

Another example is carried out to remove all the current processes of active users by employing the “pkill” command. The steps are performed below:

Currently, the active user “linux_user1” is initialized in our system. The “pkill” command can use the “-9” option to terminate all processes promptly. After that, specify the name of the active user with which you want to terminate all processes:

$ pkill -9 -u linux_user1

After exiting the above command, all processes of “linux_user1” have been automatically terminated, and the window navigates to the login window as shown below:

Example 3: HangUp the Ping Process Using pkill

Each process, when invoked, is associated with a command which initiated it. Here, we will use the exact command to identify and kill a process using the “pkill” command:

Initialize a Ping Process

To initialize the process, the “ping” command is utilized with the “google.com” as seen in the below figure:

$ ping google.com

It executes sequential processes with some millisecond time difference.

HangUp the Ping Process

The signal “-1” is utilized to reload/hang the ping process through the “pkill” command as below:

 $ pkill -1 ping

You can verify that the ping process has been hanged after executing the above command on another terminal:

The “Hangup” message at the output confirms that the pkill command has been successfully worked with the “-1” signal.

Example 4: Kill Process With Full Argument

To stop the execution, another terminal is opened and runs the below command specifying the initialized process name “ping google.com ”. The “-f” represents the full name of the process that you want to terminate:

$ pkill -f "ping google.com"

Example 5: Kill Running Processes of Multiple Users

An example is considered to kill running processes of multiple users. For instance, we have two users in the current system, execute the below script to terminate their running processes:

$ pkill -u root, itslinuxfoss

Example 6: Kill Gnome Process of Specific User

To kill the particular process of the specific user, specify the <username> with “-u” utility and name of the process. In our case, terminate the Gnome that kills all processes of the “linux_user1” user:

$ pkill -9 -u linux_user1 gnome

Example 7: Kill Most Recent Process

You can kill the most recent process by using the “– – oldest” utility with the name of the running process. For instance, the process of “firefox” will terminate after executing the below script:

$ pkill --oldest "firefox"

That’s all from this guide. We have covered Linux pkill command along with the seven efficient examples.

Conclusion

The “pkill” command stops the execution of currently active processes in the Linux operating system. It is quite useful to terminate the specified process that may be the cause of extra disk usage. The “pkill” command can be used with different options to kill processes such as “-f” (full process name), “-o” (oldest process), and “-n”(newest process). Additionally, users can utilize signals “1”, “9”, and “15” for “reloading”, “killing”, and “terminating” any process. This guide has explained “pkill” commands with their practical usage in the Linux system.