If you have worked with any Linux distribution, you know that under the hood, processes represent everything from applications to devices. Applications initiate multiple processes to cater to various functionalities.
Processes can get stuck for several reasons, including getting in a loop or waiting for input from other processes and devices. Stuck processes can freeze up UI elements and adversely affect the user experience.
That’s why Linux allows users to kill processes with various commands.
In this tutorial, we’ll discuss how to kill a process in Linux. We’ll start with a short discussion on the need for the Linux kill process. Next, we’ll go into the details of the three-step process you can use to identify and kill a process in Linux.
Table of Contents
- What is a Process in Linux?
- How to Kill a Process in Linux
- Critical Pointers in How to Kill a Process in Linux
Let’s start with a short introduction to Linux processes.
What is a Process in Linux?
In simplest terms, a process is an active instance of an application. Each process has a process ID (PID) used across the Linux system to identify and refer to the process.
As mentioned earlier, there are situations in which an active process can become unresponsive or begin using excessive volume of system resources. At this point, killing the process is the only way to improve the user experience. That’s where the Linux kill process becomes useful.
What Is the Kill Process In Linux?
Linux kill process is a set of actions used to terminate a process because it’s not responding or producing problems for other processes.
The Linux kill process uses the PID to identify and access a process for termination. Upon receiving the kill command, the OS removes (terminates) the process and the program associated with the PID passed by the kill command.
Since this is a critical process, Linux offers several ways of killing processes.
Why Is It Necessary To End A Process?
A user might need to use the Linux kill process for various reasons, including:
- The user might not be able to exit an application with the standard GUI close button for the application. This could indicate that the processes associated with the application are frozen or inactive. In this situation, the kill process command can end the program abruptly with a strong possibility of data loss.
- The user might need to utilize the kill process command to release system resources, such as memory. The processes associated with unresponsive programs can be inactive but still occupy system resources in the background. Over time, a large number of such processes can exhaust available system resources and slow down the system. The kill process command can help free up system resources by terminating these processes.
You should carefully use the kill process command because it may result in system instability or even a crash if you terminate a crucial system process.
During the process, the system sends a termination message to the process associated with the PID. The termination signal process can be one of the following:
- SIGKILL: This is the most effective Linux kill process method. Note that SIGKILL invariably results in an abrupt termination of all child processes. This could result in data loss because it leaves the process in an uncontrolled state.
- SIGTERM: Unlike SIGKILL, SIGTERM offers a more delicate way of killing a process. It can perform certain actions before exiting.
Out of these two, SIGKILL is usually the quickest and most efficient way to end the child processes. However, we recommend SIGTERM as it allows you to save the signal’s state and carry out cleanup activities.
Who can Kill Processes in the Linux Environment?
Now that you know about processes and why they get stuck, the most important question is who can kill which process in a Linux environment.
To answer this, you must think about permissions. Like everything else, each user account can access and kill specific processes.
For instance, a user can access and kill processes associated with the applications they launched. However, this doesn’t include critical system processes. On the other hand, a root user can access and kill all processes, regardless of who launched them in the first place.
How to Kill a Process in Linux
Let’s now go into the details of how to kill a process in Linux.
Methos #1: Use the top Command to Kill Linux Processes
Before killing a process, you should list all processes to identify the right process. The simplest way of listing all processes is the top command.
Use the following command to see all active processes:
This command displays the IDs of users and processes as well as information about each process’s memory and CPU utilization.
Once you have identified the process you wish to terminate, press k and enter the process ID to end it from the command’s interface. When finished, press q to close the interface and exit to the terminal.
Method #2: A Two-Step Process of Finding and Killing Processes
You can break the Linux kill process into two steps where you identify the process in the first step and then use one of the several commands to kill the process.
Step #1: Find the Process
The first step of the process is to identify the process for termination. Linux offers several ways of finding the process ID (PID) of a process.
Use the ps Command to Find a Process
The ps command is comparable to the top command in that the two commands display similar information about active processes.
However, the ps command doesn’t have the top command’s interface. Instead, the command displays a comprehensive list of all active processes.
You can further format the command’s output by adding flags that change the command’s behavior. The most popular flags that present the most information about processes are:
- -a: See the processes of all users instead of just the currently logged-in user.
- -u: Gives thorough details about the processes.
- -x: Includes the daemon-controlled processes instead of user-controlled ones.
As such, the ps -aux command provides comprehensive information about all processes.
Use the pgrep or pidof to Find the PID
pgrep is a simple command for returning process information through a partial pattern. For instance, the command uses a regular expression (a*) as the wildcard pattern. The syntax of this command is:
pgrep <flags> <pattern>
You can use the following flags with the pgrep command:
- -l: Lists the PIDs and the names of the processes.
- -n: Returns the most recent process.
- -o: Returns the oldest process.
- -u: Returns the processes associated with a particular user.
- -x: Returns the processes that precisely fit the specified pattern.
A typical use case for this command is the following command that lists all processes by the root user that contains an a at the start of the process’s name.
# pgrep -u root 'a*'
If you know the process’s name, you can use the pidof command to find its PID. The syntax of the command is:
# pidof <flags> <program>
This command supports the following flags:
- -c: Provide PIDs for the processes for the root user.
- -o: Leave out some PIDs.
- -s: Returns a single PID.
- -x: Includes the PIDs of scripts executing in the shell.
Step #2: Kill the Process With the Kill Command Options
Depending on whether you know the process name, PID, or just how long the process has been running, there are a few different ways to kill a process in Linux. We’ll now discuss six commands that you can use to kill processes.
The killall Command
Use the killall command to end a process by name. This command issues a SIGTERM signal by default. This command can kill several processes in one go.
The syntax of this command is:
# killall <process>
You can use the following flags with the command:
- -e: Looks for the process name’s exact match.
- -I: Disregard the case when attempting to locate the process name.
- -i: Requests confirmation before killing the process.
- -u: Kills the processes that belong to a particular user.
- -v: Provides an update about the success or failure of the process
The killall command can be used to kill processes by age (time duration). For this, use the following flags with the command:
- -o: Mention a time duration with this flag to terminate all processes that have been running for longer than that period.
- -y: Mention a time duration with this flag to terminate all processes that have been running for less than that period.
For instance, the killall -o 15m command will kill processes that are more than 15 minutes old. Similarly, the killall -y 15m command will kill processes that are less than 15 minutes old.
The pkill Command
The pkill command is similar to the pgrep command in that it will kill a process after finding the process based on specific qualifying criteria. The command issues a SIGTERM signal by default.
The syntax of the command is:
# pkill <flags> <pattern>
You can use the following flags with the pkill command:
- -n: Only terminates the most recent process it finds.
- -o: Only terminates the oldest processes.
- -u: Only terminates the processes that belong to a specific user.
- -x: Only terminates the processes that precisely fit the mentioned pattern.
- (signal): Instead of SIGTERM, send a different signal to the process.
The kill Command
If you know the process id, use the kill command to terminate the process. The syntax of this command is:
The kill command ends the specified processes one at a time. It notifies a process to stop by sending out a SIGTERM signal. It waits for the shutdown process of the program to complete.
You can send a different signal with the -signal flag.
The kill -9 Command
The kill -9 command is useful when you need to kill an unresponsive service. You can either execute it like any other kill command by passing the PID:
# kill -9 <processID>
Or send the SIGKILL signal to the process for immediate termination.
# kill -SIGKILL <processID>
A service that receives the kill -9 Linux command is instructed to stop working immediately (with the SIGKILL signal). A nonresponsive process might not send any feedback but terminates on receiving a kill -9 instruction. Note that the command doesn’t follow the standard shutdown process and thus can result in data loss.
The xkill Command
The xkill command is typically used to cut off a server’s communication with clients. The syntax of the command is:
# xkill <resource>
Critical Pointers in How to Kill a Process in Linux
Keep the following in view when running a command to kill processes:
- If there is no other way, you can kill a process manually from the command line.
- You should always find the PID for processes you wish to terminate. This ensures that you don’t accidentally terminate a system process and crash the server. We recommend using top, ps, pidof, or pgrep commands.
- Once the process you wish to terminate is located, you can use the killall, pkill, kill, xkill, or top commands to end it.
- You can use the SIGHUP, SIGKILL, or SIGTERM termination signals to initiate the Linux kill process.
- Use the sudo command to ensure you have the proper permissions for terminating a process.
Knowing how to kill a process in Linux is key to maintaining a stable and responsive Linux system.
You can use commands such as kill and pkill to send specific termination signals to processes. Note that some commands can force-terminate processes, resulting in data loss.
RedSwitches offers the best bare metal infrastructure for your Linux projects. Get performance, dependability, and assistance at market-competitive prices. We offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.
Start now and put your trust in our expert support team.
Q. What is the purpose of killing a process in Linux?
Killing a process in Linux is essential for various reasons, such as terminating unresponsive or malfunctioning applications, managing system resources, and maintaining system stability.
Q. What is the difference between the “kill” and “pkill” commands?
The kill command sends default signals to specific processes using their process IDs (PIDs), while “pkill” allows you to terminate processes by name, making it more user-friendly.
Q. What signal is sent by default when using the “kill” command?
By default, the kill command sends the SIGTERM signal, allowing the process to perform cleanup operations before termination.
Q. How can I forcefully terminate a process that is not responding to the SIGTERM signal?
To forcefully terminate a process, use the kill command with the SIGKILL signal (signal number 9).
Q. Are there any risks associated with killing processes in Linux?
Yes, killing processes should be done carefully. Terminating system-critical processes or critical applications may lead to system instability or data loss. It’s essential to identify the correct process to terminate.
Q. Can I automate the process of killing specific processes in Linux?
Yes, you can create scripts or use tools like pkill in combination with other commands to automate the process of terminating specific processes.
Q. Is it possible to send custom types of signals to processes using the kill command?
Yes, you can send custom signals by specifying the signal number when using the kill command. This allows for more fine-grained control over the termination process.
Q. How can I check which processes are running on my Linux system before deciding which ones to kill?
You can use the ps command to list running processes. This provides information about their PIDs, resource usage, and status.
Q. What steps should I take after killing a process to ensure system stability?
After killing a process, it’s crucial to monitor the system’s behavior and address any issues that may have led to the process’s misbehavior or unresponsiveness.
Q. Are there alternatives to killing processes for managing system resources in Linux?
Yes, you can use resource management tools like nice and renice to adjust process priorities or consider increasing system resources if possible to avoid killing processes.