The pkill command | Explained

The pkill command is typically used to terminal or stop the processing of a certain process in a Linux-based or Unix-based operating system. However, the pkill command allows the user to send specific signals to the process based on which the termination of the process is dealt with.

If you are a Linux beginner or a Linux System Administrator, then the pkill command is something that you will be using a lot to deal with unwanted processes. This article will explain all of the information that you need to understand and master the usage of the pKill command for Linux / Debian-based operating systems.

The pKill Command for Linux and macOS

The pkill command is available on Linux and Unix (macOS) operating systems. This command is used to end the processing of a certain process. This command searches the process table and sends a termination signal to stop a process that meets the criteria defined in the command parameter.

To better understand what the criteria mean, let’s go over the syntax of the pkill command.

How to use the pkill Command in Linux/Mac?

Unlike other kill commands, the pkill command doesn’t require a process ID. Rather, the user can provide a pattern (mostly name) of the process, and the matching process will be terminated. Take a look at the syntax of the pkill command.

pkill [Option_Param] [Pattern_Param]

In this syntax:

  • The pkill command takes different options that change how a program is terminated. For example, stopping the execution of a process or forcing it to terminate without completing any exit sequence.
  • The pattern parameter takes an extended regular expression, which is mostly the name of the process, and based on this parameter, the process is terminated.

Example of the pKill Command

If you are running the browser (firefox) and want to stop, terminate it using the pkill command. Then simply open up a terminal and type the following command inside it:

pkill firefox

With this, you have successfully terminated the Firefox browser.

Different Signals of the pkill Command in Linux/Mac

If no options are specific, the termination signal will be given to the process by default. However, you can pass different signals depending on what you intend to do with the running process. Take a look at the different signals that can be passed to the pkill Command.

Signal CodeSignal NameDescription
-1SIGHUPReloads Configuration File
-2SIGINTSends a Keyboard Interrupt (CTRL + C)
-9SIGKILLKill Process Signal
-15SIGTERMEnds the process immediately. (Terminate a process in a controlled manner so cleanup is possible)
-18SIGCONTContinues the processing of a stopped process.
-19STOPStop the process (Halts execution)

To use these signals, you can type their name or their signal code, and after that, simply specify the process to send this signal.

DraculaServers’ KVM VPS Hosting!

Take control of your online presence with DraculaServers’ KVM VPS Hosting. Our cutting-edge technology gives you the freedom to customize every aspect of your virtual server environment, ensuring a hosting experience that’s uniquely yours. Experience lightning-fast speeds, scalable resources, and an intuitive control panel—all at competitive pricing.

Don’t miss out! Visit Dracula Server’s KVM today and discover the hosting plans to empower your online journey.

Ready to take charge? Check out our KVM Hosting Plans Here!

How to Force Kill a Process With the pkill Command?

To ensure the process is terminated without having the system block or ignore the stop command, you can utilize the “-9” signal code within the pkill command.

For example, let’s run the “top” command in a new terminal session:

The pkill command | Explained - Dracula Server

Let’s kill this “top” process using the following command in a new terminal window:

pkill -9 top

When this code is executed, the terminal session with the top command running shows the following message and stops the top process:

The pkill command | Explained - Dracula Server

You have successfully force-killed a process using the pkill command.

How to Kill the Parent Process Using the pkill Command?

Oftentimes, processes are running in a sequence (having multiple instances, each performing a different function). An example of this can be running multiple tabs and windows inside the Firefox browser. In such a case, if you want to close all of the subprocesses of the Firefox browser, then it is best to kill the main parent process.

The parent process is called the oldest process, and in the pkill command, you can use the “–oldest” flag to kill it. First, let’s confirm that there are multiple instances of Firefox process in this certain scenario:

The pkill command | Explained - Dracula Server

After that, open up a terminal and type the following command:

pkill --oldest firefox

After the execution of this command, all of the subprocesses of the firefox have been terminated. This can be confirmed by typing the pidof firefox command:

The pkill command | Explained - Dracula Server

You have successfully terminated the parent processes using the pkill command.

More Details About the pkill Command

If you want to learn all the different options about the pkill command, then simply visit the manual page of the pkill command. To do this, open up a new terminal session and type the following command inside it:

man pkill

This will show its manual page that contains information about the pgrep, pkill, and the pidwait command:

The pkill command | Explained - Dracula Server

That’s about it for this tutorial.

Conclusion

The pkill command can be used in linux and macOS to send a specific signal to a process or to terminate its execution. The processes are identified within this command by using an extended regular expression, which generally is the name of the process. The pkill commands also come with different options that allow the user to choose which signal to send to the process for stopping or terminating its execution.

Check out more Linux Tutorials Here!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments