Kill Process in Linux using kill Command Types

How to Kill Process in Linux using kill Command Types

Until now, you must have been in a situation where you have launched a program in the Linux operating system and while you are using the program, it suddenly crashes and remains unresponsive. In this situation, most users stop and restart the program, but still, the program froze up. In such a situation, users do not know what to do to run the program correctly and do not know the cause of the failure. When you stop and restart the application, the application will not run correctly because the main request process is not completely stopped.

In the Linux operating system, running tasks are known as Processes. To solve this problem, the system must either be restarted or if your program still froze up after restarting the system, the Process must be killed or stopped. but how?

As in Windows, the task manager can manage problems similar to the problem of unresponsive processes and programs, fortunately, in Linux, there are various methods and services to kill and stop unresponsive processes.

It is also possible to kill processes in Linux through the command line (pressing CTRL+C keys) and the graphical user interface, but it will be easier to do this task using the command line. Linux is an advanced and excellent operating system that provides a solution for every problem; don’t worry about anything when using Linux VPS because your problems will be easily solved, you just need to improve your knowledge and awareness about using Linux and be familiar with useful Linux commands. By learning how to kill processes in Linux, you can manage your Linux VPS better and more efficiently.

In this article, we will teach how to kill incorrect processes in Linux using various Kill commands, which is one of the most common Linux commands. You will also learn how to terminate a process that is not working properly or is consuming too much system resources.

How to kill processes in Linux?

You can terminate the wrong processes by identifying the running processes and accessing information about each process, as well as by entering a variety of Kill commands. One thing you should pay attention to is that to kill all processes, you must have root user permission or sudo privileges, otherwise, as a normal Linux user, you can only kill your own processes and you won’t be able to kill processes that belong to other Linux users.

Different signals are sent to terminate a specific process, the most common of which are SIGKILL and SIGTERM. The SIGTERM signal tries to kill the process and sometimes fails to terminate it, but the SIGKILL signal is more serious and risky because it quickly kills any type of process and leaves no room for retrying to terminate the specific process or the possibility of its blocking.

What are the uses of the kill command?

  • It Shuts down the server or the program that you have already started and forgot.
  • ItTerminates the incorrect running process.
  • It Eliminates the process that consumes too much system resources.
  • It Kills the process that is running automatically.
  • Terminating a process that was accidentally executed.
  • It Eliminates the background process.
  • Sending a signal to the process to terminate the process.

Viewing information about running processes in Linux

  • Viewing information about running processes using the top command

To execute the kill command to stop the process, you must first have access to a list of running processes in Linux and their information. To see a list of running processes, run the following command:

top

The output of the above command displays a list of processes along with process IDs and users and information about the amount of memory used and CPU power.

Thetopcommand interface allows you to terminate a process by pressing the K button and typing the desired process ID, without executing another command from the provided list.

Note: Press the q button to exit the top command interface.

  • Viewing information about running processes using the ps command

The ps command finds all the running processes in Linux and presents them in a list format. The difference between the output of the ps command and the top is that the ps command does not act as an interface, but it displays complete information from PIDs, CPU power, and other details from running processes. The main syntax of thepscommand is as follows:

ps <options>

Also, the ps command provides Linux users with customized details of the running processes, along with the tags that we will mention below. One of the common options to display more information about running processes is “-aux“, which is executed in the form of the following command:

ps -aux

The options that can be executed with thepscommand includes the following:

OptionsDescription
-aIt provides access to all running processes instead of being limited to the processes belonging to the current user.
-uProvides more details about each process.
-xProvides processes that are managed by daemons.

To access specific information about running processes in Linux, such as displaying java running processes, you can also use the following command combination:

ps -ux | grep java
  • Finding PID and process details using the pgrep command

In addition to the previous method, by executing the top and ps commands, users could access a list of running processes. There are other ways to search for specific processes along with their information. Linux users can find the process ID number (PID) or process name by running various commands and then killing them.

Using thepgrepcommand is not as simple as the previous commands, but it is a way that users can customize specific patterns and criteria to find a specific process. The main syntax of thepgrepcommand is as follows:

pgrep <options> <pattern>

Note: In the pattern that is defined, the symbol * is general, and a* is one of the widely used options that is used as the pattern expression.

For example, if you want to access the processes managed by the Root user, run the following command:

pgrep -u root

If you want to find processes that start with the letter “a” and belong to the Root user, the following command will get you what you want:

pgrep -u root 'a*'

Along with thepgrepcommand, you can use other options to obtain specific information from Linux processes, which include the ones we present in the table:

OptionsDescription
-nDisplays the newest process.
-lProvides a list of PIDs and process names.
-oDisplays the oldest process.
-xIt provides only processes that meet the criteria defined in the pattern.
-uYou can search for processes belonging to specific users.
  • Obtaining the process ID number using the pidof command

You can also use thepidofcommand to view the information and PIDs of the processes, with the difference that you must know the names of the processes to access the PIDs of the processes. The main syntax of thepidofcommand is as follows:

pidof <options> <program>

You can execute different options for different purposes with thepidofcommand; we presented these options in a table:

OptionsDescription
-cThe provider of PIDs is in the Root directory.
-sIt displays only one PID.
-oYou can kill a process by typing its PID after flag-o.
-xThis option displays the PID of the shells that execute the scripts.

How to use all types of Kill commands to terminate processes

At the beginning of the article, we explained that the Kill command sends a signal to terminate the processes. To terminate processes, there are various signals in Linux, to see the signals of the Kill command, you must run the following command:

kill -l

In the output of the kill -l command, various signals are displayed along with their names and numbers, but as we mentioned at the beginning of the article, usually two signals SIGKILL (9) and SIGTERM (15) are sent.

The Kill command has various uses in terminating processes. In the following, we will introduce you to the types of Kill commands and their applications in killing processes.

  • Using the Kill command to terminate processes with PID

After accessing the process ID, you can kill a specific process by executing the Kill command:

kill <processID>

It should be noted that the Kill command automatically uses the SIGTERM signal to terminate processes, but you can use the following command to change the default signal:

-signal
  • Using the command kill -9 to kill processes

If thekill <processID>command fails to kill the desired process, you can use the following command as an alternative, having the PID of the specific process:

kill [Signal_or_Option] pid

In the previous command, you can use the desired option or signal to definitely kill the processes and in this way determine a specific signal.

The shorter form of the previous command can also be executed as follows:

kill -9 <processID>

Or

kill -SIGKILL <processID>

Executing the commandkill -SIGKILL <processID>or kill -9 <processID>, will definitely destroy the incorrect process that the commandkill <processID>could not terminate. Executing the previous commands bypasses the standards of shutting down processes and destroys any kind of process and data. The SIGKILL service kills all processes, if this does not happen, you should suspect a malfunction in your operating system.

  • Running the Kill command to kill multiple processes

Users can kill several processes at the same time using the kill command:

kill -9 pid1 pid2 pid3
  • Running the killall command to kill processes by name

When killing unresponsive processes, if you don’t have the process ID numbers, but the process names are available, running thekillallcommand is useful. Several processes can be killed by executing thekillallcommand, whose main syntax is as follows:

killall <process>

Along with theKillallcommand, you can also use other options:

OptionsDescription
-eIt looks for an exact match with the process name.
-IIt is used to eliminate attention to capitalization and lowercase letters of the process name.
-iUsed to add additional confirmation to kill the process.
-vProvides a report to ensure the successful termination of the process.
-uTerminates processes belonging to a specific user.
-oFlag-o along with a specific time period, kills the processes that are executed more than this specific period of time.
-yFlag-y along with the specified time kills processes that have been executed for less than the specified time.

One of the advantages of thekillallcommand is the ability to customize the timestamp for terminating processes. For example, you can specify that the process that is running for less than 20 minutes will be killed with the following command:

killall -y 20m [Process_Name>]

For example, if you want to kill processes that run for more than 20 minutes, you should run the following command:

killall -o 20m [Process_Name>]

As you know, the components s (seconds), m (minutes), h (hours), d (days), w (weeks), M (months), and y (years) can be placed in the previous command and terminate processes based on their age.

Note: It should be noted that the Killall command uses the SIGTERM signal by default to kill processes.

  • Using the xkill command to block the connection between the server and the client

This command is used to close the processes that are running unintentionally on the server, in fact, it prevents the connection from the server to the client. The syntax of thexkillcommand is as follows:

xkill <resource>

Running thexkillcommand without specifying the source allows the process to be closed by selecting them in a window.

  • Using the pkill command to kill processes by name

The pkill command is able to kill unresponsive processes using a specific pattern or process name. Thepkillcommand, like thekillallcommand, uses the SIGTERM signal to kill processes. The difference betweenpkillandkillallis thatpkillmatches a specific pattern, whilekillallkills the process based on its name.

The main syntax of thepkillcommand is as follows:

pkill <options> <pattern>

For example, to kill the Firefox browser process, you can run the following command:

pkill Firefox

The previous command is useful when you know the full name of the process. If you remember the partial name of the process, use the following command instead of the previous command:

pgrep -l Fir

In the following, we will present the options that are used using thepkillcommand:

OptionsDescription
-uIt kills the processes that were executed by the specified user.
-nIt kills the newest processes.
-oIt terminates the oldest processes.
-signalIt changes the default signal of the pkill command (SIGTERM).
-xKills processes according to the pattern.

How to kill the running process in Linux via GUI?

For some users, it is easier to apply the settings through the GUI, that’s why they prefer to apply the Linux settings through the GUI. You can use the Linux GUI to kill the running process. Fortunately, most Linux distributions provide Gnome, KDE, or another graphical user interface by default, so that users can freely decide between the terminal environment and GUI to perform their tasks. Follow the process below to kill the running process through the GUI:

  • Find the Task Manager or the system monitor in the system menu of your favorite Linux desktop environment.
  • Click on the process tab. You will come across a page containing a list of running processes. Select the process you want to terminate. You can search for the process related to the program you want through the magnifier (search) icon.
  • By selecting the running process, click on the End Process button to kill the process. You can also terminate the process by right-clicking on the running process and selecting the Kill/End option from the context menu.

 Kill Process in Linux via GUI

 

Note: On the page, you are viewing, in the process tab, you can also access information such as the process ID of the running processes.

As a result, you succeeded in killing the running process through the GUI.

FAQ

By executing thekill-3command, you can get a list of Java threads in the Java virtual machine. The command kill -3 works as a thread dump.

By executing different types of kill commands, a signal is sent to the running process to stop the desired process. Before executing the kill command, you must access the information and ID of the processes and then terminate the wrong processes by executing the kill command.

If you want to create an interrupt, you can enter theKill -2command, in this case, the process that is not working properly will be shocked and then the loop will continue. The number 2 in the command Kill -2 is the same value set for SIGINT. You must send 15 (SIGTERM) to stop the process completely.

When the service becomes unresponsive in Linux, run the kill, killall, and pkill commands to terminate the program. You can also use the ctrl+c shortcut key to close the program that has frozen.

Conclusion

It is very easy to kill processes that are not running properly in Linux by executing various kill commands. When you cannot stop a process in other ways, for example through the GUI or rebooting the system, the most efficient way is the Linux terminal. In this article, you learned that to terminate a process in Linux, SIGKILL or SIGTERM signals, which are the most widely used, are sent.

The first step you must take to kill a process is to access the information and PID and names of the running processes, for this purposetop,ps,pidoforpgrepcommands can be used, and after knowing the name and ID of the processes, you can kill the desired process by executing the commandskillall,pkill,kill,xkill.

The types of Kill commands are similar to each other and the difference is that the Kill command kills the processes using their PID, but the Killall and pkill commands terminate them using the names of the processes and other criteria. Also, their other difference is in the root package of the commands, The ps, top, kill, pkill commands belong to the procps package, while the killall command belongs to the psmisc package.

Each type of kill command is used for specific purposes and is the fastest and safest way to solve your problem of killing incorrect processes. We hope that reading this article has solved your problem.

Thank you for your choice.

Leave a Reply

Your email address will not be published. Required fields are marked.