×

How to Kill Linux Process Using Kill, Pkill, and Killall

How to Kill Linux Process Using Kill, Pkill, and Killall


In Linux, killing a process refers to terminating or stopping the execution of a running program. Each running process is assigned a unique identifier number known as the Process ID (PID), which helps the system to keep track of currently active processes.

In this article, we’ll find out about the Linux kill process and how to find and terminate a process using different tools and commands in Linux.

What Is the Linux Process

In simple terms, a process is like a program that’s currently running on a system, and each process has its own special ID (PID) number. However, sometimes a running program, or process, can misbehave or might stop responding or use up too much of the system resources.

When that happens, we need to stop or “kill” the process and this is where the kill command comes in handy.

What Is Kill Command

The “kill” command is a crucial utility that allows users to send signals (stop or terminate) to currently running processes, instructing them to gracefully exit or forcefully terminate.

The kill command is useful when you need to manage and control the activities of different programs on your Linux system such as:

  • To stop any automated process.
  • To stop a process that has been started by accident.
  • To stop a process that uses too much memory.
  • To forcefully terminate any running process in Linux.
  • To stop a process running in the background.

Using the kill command from /usr/bin provides you with some extra features to kill a process by process name using pkill, which identifies processes by their names.

How to Find Process ID or Process Name

Before terminating a running process, it’s essential to identify its Process ID (PID) or name using the following ps command that displays information about all running processes on the system with their PID (process ID) and other information.

$ ps -ef
List All Running Processes

If you want to find a specific process name PID, you can use a grep command that will list all processes running on the system and filter the results to show only those containing the term “mariadb“.

ps -ef | grep mariadb

Alternatively, you can directly find the PID of a process by name using the pgrep command as shown.

pgrep mariadb
Find Process PID
Find Process PID

How to Kill a Process in Linux

Before you kill, stop, or terminate a process, think about permissions. If you’re a root user, you can stop any process, but if you are a normal user, you need to add a “sudo” before the command or switch to a root with “su” to use the termination command.

The common syntax for the kill command is:

kill [signal or option] PID(s)
OR
sudo kill [signal or option] PID(s)

For a kill command a Signal Name could be:

Kill Command Signals
Kill Command Signals

Clearly from the behavior above:

  • SIGTERM is the default and safest way to kill a process.
  • SIGHUP is a less secure way of killing a process than SIGTERM.
  • SIGKILL is the most unsafe way among the above three, to kill a process that terminates a process without saving.

In Linux, there are different signals you can use to stop, end, or pause processes. You can list all available kill signals with their names and corresponding numbers using the following command, which will list

kill -l

While there are multiple available signals, but in most cases we only use SIGKILL (9) and SIGTERM (15).

List Kill Command Signals
List Kill Command Signals

To kill a process, we need to know the Process ID of a process. A Process is an instance of a program. Every time a program starts, automatically a unique PID is generated for that process.

Every Process in Linux has a pid. The first process that starts when the Linux System is booted is the – init process, hence it is assigned a value of ‘1‘ in most cases.

Init is the master process and can not be killed this way, which ensures that the master process doesn’t get killed accidentally. Init decides and allows itself to be killed, where kill is merely a request for a shutdown.

Before we step ahead and execute a kill command, some important points to be noted:

  • A user can kill all his processes.
  • A user can not kill another user’s process.
  • A user can not kill processes the System is using.
  • A root user can kill the system-level process and the process of any user.

To kill the mariadb process PID, use the kill command as shown.

kill -9 3383

The above command will kill the process having pid=3383, where PID is a Numerical Value of the process.

Another way to perform the same function can be rewritten as.

kill -SIGTERM 3383

Similarly ‘kill -9 PID‘ is similar to ‘kill -SIGKILL PID‘ and vice-versa.

To confirm that the process has terminated, you can again use the ps command.

ps -ef | grep mariadb
Kill Linux Process with PID
Kill Linux Process with PID

How to Kill Multiple Processes in Linux

To terminate multiple processes in Linux using their Process IDs (PIDs), you can use the kill command in combination with the relevant PID numbers.

First, identify the PIDs of the processes you want to terminate using the ps or pgrep command.

ps aux | grep apache2
OR
pgrep apache2

Once you have the PIDs, use the kill command to terminate them.

kill -9 PID1 PID2 PID3

How to Kill a Process in Linux Using Process Name

To terminate a process using the process name, we will use the pkill command, which is a version of the kill command that allows you to mention the process name or a pattern to locate a process.

You must be aware of the process name, before killing, and entering a wrong process name may screw you.

pkill mysqld

What if a process has too many instances and several child processes, we have a command ‘killall‘ that takes the process name as an argument in place of the process number.

How to Kill a Process in Linux Using the Killall Command

The main distinction between killall and kill is that killall can end a process by its name, whereas the kill command relies on the process ID (pid).

To kill all mysql instances along with child processes, use the command as follows.

killall mysqld
OR
pkill mysqld

You can always verify the status of the process if it is running or not, using any of the below commands.

systemctl status mysql
pgrep mysql
ps -aux | grep mysql

Linux graphical systems monitor tools like htop or gnome-system-monitor provide a user-friendly interface to find and kill processes.

Conclusion

That’s it for now from me. I’ll be back soon with another interesting and informative topic. Until then, stay tuned and connected to Tecmint, and take care of your health. Don’t forget to share your valuable feedback in the comments section.



Source link