It could be necessary to stop a Linux process if it becomes unresponsive or uses too many resources.

Most processes have unique ways of terminating. Unfortunately, malfunctioning processes sometimes refuse to stop. Therefore, it becomes important to use a command to kill a background process if it becomes unresponsive.

In this article, we will explain how to kill a process in Linux.

Step 1: examine the active Linux processes

The top command makes it simple to inspect all of the processes that are active at the moment.

Use the following command to see a list of all processes that are active right now:

top

The process IDs and users, as well as the memory and CPU usage per process, are all displayed by the top command.

Press k and the process ID to stop processes directly from the top interface. Press q to leave the top interface.

Step 2: Locate the process you want to kill

Before killing a running process in Linux, you must first locate it. In Linux, you can look for a process in various methods. 

Process names (or partial names) or process IDs, commonly called "pids," can be used to locate a process.

Use the ps command to find a process.

The ps command also shows similar information to the top command but without an interface. 

Instead, the ps command offers an exhaustive list of active processes formatted according to your set tags.

ps <options>

For instance, the ps -aux command will thoroughly list every process.

Using the pidof and pgrep commands to find the process

We may find the process by using the pidof command. The pidof command will be quite helpful if we want to monitor a process based on its name. 

When combined with the process name, it shows the PIDs of the running processes. Use the pidof command to find a process' PID as shown below:

pidof process_name

Killing a running process in Linux

After locating the process, it’s time to kill a process in Linux. A process can be terminated with several commands, including kill, kill, top, and pkill.

Knowing which processes we can kill is crucial before killing a process. The following are some crucial points regarding killing processes:

  • If you are a typical user, you can only terminate your processes. The processes that other users are using cannot be killed. The users and associated processes are displayed by the top and ps aux commands.
  • A root user can terminate all processes. To run a command as root, we may also put sudo before it.
  • A unique signal is sent to the processes in Linux when a process is terminated. Linux handles the SIGKILLS and SIGTERM signals in this situation, even though it supports many other signals. Linux delivers the SIGTERM signal by default, which gracefully kills the process. Use SIGKILL instead if you want to end a process forcefully.
  • Until a process is eliminated from the system, Linux retains its status. As a result, until a parent process is deleted, the child processes will be visible in the process list.
  • If a process is in the " uninterruptible sleep" mode, we cannot terminate it. 

Kill a process in Linux by the killall command

You can use the killall command to end processes one by one. It will automatically deliver the SIGTERM signal. Multiple processes can be terminated simultaneously with the killall command.

killall <process>

The killall command can be used to kill processes based on their age and name, among other criteria.

The killall command has several options that can be applied:

-e. Look for a process name match that is exact.

-I. Ignore case while attempting to discover the process name.

-i. Ask for extra confirmation when killing the process.

-u. Only stop processes that belong to a certain user.

-v. Provide feedback on whether the procedure was able to be stopped.

Kill a process by the pkill command

The pkill command will be the most helpful tool to kill a process in situations where we don't know the specific name of the process. By typing the correct process name, we may kill a particular process.

The pkill command does seem to be similar to the pgrep command in that it will kill a process in addition to other qualifying criteria based on the process name. Pkill automatically sends the SIGTERM signal.

pkill <options> <pattern>

Options of pkill include:

-n. Only eliminate the most recent processes that are found.

-o. Only terminate the oldest processes that have been found.

-u. Only terminate processes that belong to the chosen user.

-x. Only terminate processes that exactly match the pattern.

-signal. Instead of SIGTERM, send the process a specified signal.

Killing a running process in Linux via kill Command

The kill command is the simplest tool to kill a process in Linux; all that is required is the process ID.

kill <processID>

A single process with the specified process ID will be terminated at a time by the kill command. It will notify a process to stop by sending a SIGTERM signal. It waits for the program's shutdown procedure to finish.

To provide a signal other than SIGTERM, use the -signal command.

Kill a process in Linux using kill -9

The command kill -9 can be used to terminate an unresponsive service. Run it in the same manner as a standard kill command:

kill -9 <processID>

or 

kill -SIGKILL <processID>

The kill -9 command instructs a service to terminate quickly by sending a SIGKILL signal. An unresponsive program will disregard a kill command, but a kill -9 command will result in its shutdown. 

Use caution when using this command. Because it skips the typical shutdown procedure, any saved data will be destroyed.

If a SIGKILL signal does not terminate a service, your operating system is malfunctioning.

Killing a running process in Linux by System Monitor

Linux's system monitors the graphical environment and also allows us to end a process. The actions listed below should be followed to terminate a process using a system monitor:

First, press Enter to access the system monitor and use it to search or explore. Then, your Linux system's active processes will be listed.

Right-click the process you want to end by browsing through it. Choose the Kill option in step four. Additionally, we can end it by using CTRL+K.

Kill a process in Linux via xkill command.

A unique kind of command called xkill cuts off client connections to a specific server.

xkill <resource>

xkill will terminate any undesirable processes that a server may have opened.

If xkill is executed without a resource, an interface allowing the user to choose which window to close will be displayed.

Conclusion

You might need to end a Linux process if it stops responding or uses too many resources.

The majority of processes have unique ways to end. Unfortunately, malfunctioning systems can resist being shut off. When a background process becomes unresponsive, a command must be used to end it.

We discussed many methods for killing processes in Linux in this article. To manage and administer systems, these Linux termination commands must be learned and comprehended. You can also buy linux vps to practice it on a routine basis.

People also read: