How to Kill a Process in Linux from Command Line?
Posted on
When dealing with a Linux server, you may encounter unresponsive or malfunctioning processes that can hog system resources, impede your workflow, and disrupt other running processes. Understanding how to terminate such a process in Linux is crucial for effective system administration.
If a Linux process becomes unresponsive or consumes excessive resources, it may need to be terminated. Every process has its own unique way of shutting down. Regrettably, processes may experience malfunctions and necessitate the execution of a command to terminate them manually.
This guide provides step-by-step instructions on terminating a Linux process using the command line.
PRODUCTS THAT MIGHT INTEREST YOU:
Benefit from the best server plans and related services, competitive prices, coupled with personalized attention to each client. Supported by top-notch technical assistance that remains consistently accessible to address all your inquiries.
-
Epic VPS
from $43.00 /month
-
Ukraine dedicated servers
from $68.00 /month
-
Ukraine 10G servers
from $680.00 /month
What Does a Linux Process Mean?
Put simply, a process is an active instance of an application. Every process in the Linux system is assigned a unique process ID (PID) that is used to identify and reference the process.
As previously stated, there may be instances where a running process becomes unresponsive or starts consuming excessive system resources. Terminating the process is the sole method to enhance the user experience at this stage. That’s when the Linux kill process comes in handy.
Understanding the Kill Process in Linux
Terminating a process in Linux involves a series of actions aimed at resolving unresponsiveness or issues caused by the process for other processes.
The Linux kill process utilizes the PID to identify and access a process for termination. When the kill command is received, the operating system terminates the process and the program linked to the PID provided by the kill command.
Given the importance of this process, Linux provides multiple methods for terminating processes.
Why is it important to terminate a process?
There are several reasons why a user might need to utilise the Linux kill process:
Exiting an application may not be possible using the standard GUI close button for the application. It seems that the processes linked to the application are currently frozen or inactive. In this scenario, using the kill process command may result in the program being terminated abruptly, potentially leading to data loss.
One possible solution is to use the kill process command, which can help free up system resources like memory. Processes linked to unresponsive programs can remain idle while consuming system resources in the background. Over time, an abundance of these processes can deplete system resources and cause a decrease in system performance. The kill process command can be used to terminate processes and free up system resources.
It is important to exercise caution when putting to use the kill process command, as terminating a critical system process can lead to system instability or even a crash.
Throughout the process, the system sends a termination message to the process linked with the PID. There are several options for the termination signal process:
SIGKILL is considered the most efficient method for terminating processes in Linux. It’s important to keep in mind that SIGKILL will always cause an immediate termination of all child processes. There is a risk of data loss as the process is left in an uncontrolled state.
SIGTERM: In contrast to SIGKILL, SIGTERM provides a more gentle approach to terminating a process. It is capable of executing specific tasks prior to termination.
Typically, SIGKILL is the fastest and most effective method to terminate the child processes. However, we suggest using SIGTERM as it enables you to preserve the signal’s state and perform necessary cleanup tasks.
Who has the ability to terminate processes in the Linux environment?
Understanding processes and their termination is crucial in a Linux environment.
In order to address this question, it is necessary to consider permissions. Just like any other feature, every user account has the ability to access and terminate certain processes.
For example, a user has the ability to access and terminate processes that are linked to the applications they initiated. However, this does not cover essential system processes. However, a root user has the ability to access and terminate all processes, regardless of their origin.
Requirements
- A Linux-based system.
- A user account with administrative privileges.
- Access granted to the terminal.
Methods for Identifying Process ID or Process Name
Prior to terminating a process, it is essential to first locate its whereabouts. Processes can be located using either the process name (or a partial process name) or the process ID (also referred to as a PID).
There are various methods to locate a process in Linux:
- Using the ps command.
- You can use the pgrep or pidof command.
- Using the top command.
Below, you’ll find various methods for searching for a process in Linux.
Find a Process using the ps Command
The ps command offers a comprehensive overview of active processes, with customisable formatting options. Here is the syntax for the ps command:
ps [options]
Here are some frequently used options to add to the “ps” command:
-a. Display processes from all users instead of only the current user.
-u. Provide detailed information about each of the processes.
-x. Incorporate processes that are managed by daemons rather than users.
Take the command ps -aux as an illustration, which provides a comprehensive list of all processes:
Discover the PID using either pgrep or pidof
Using the pgrep Linux command provides a more intricate approach to locating a process. The pgrep command retrieves processes based on specific selection criteria, referred to as the pattern. The pattern is a regular expression, like a*, where * acts as a wildcard.
pgrep [options] [pattern]
Here are the available options for pgrep:
-l. List the process names and the PIDs.
-n. Return the newest process.
-o. Return the oldest process.
-u. Only find processes that belong to a specific user.
-x. Only find processes that exactly match the given pattern.
As an AI experts, we can show you an example of how to display the names and PIDs of all processes owned by root using the command pgrep -l -u root:
With the pidof command, you can easily retrieve the ID of a process by specifying its name.
pidof [options] [program]
Here are some of the options that pidof accepts:
-c. Return only the PIDs located within a single root directory.
-o. Omit certain PIDs (include the processes to omit after the flag).
-s. Only return a single PID.
-x. Additionally provides the process IDs of active shells executing scripts.
For instance, use the following command to obtain the snapd process’ PID: pidof snapd
Check out the running processes using the top command.
The top command provides a comprehensive overview of the currently running processes. To see a comprehensive list of currently running processes, execute the following command:
top
When the top command interactive mode is initiated, it provides a comprehensive display of process IDs, users, memory and CPU usage, running time, and more.
To exit the main interface, simply press the q key.
How to Terminate a Process
It is important to take permissions into account prior to terminating or killing a process. A root user has the ability to terminate all processes. To run a command as root, you have the option of using sudo or obtaining a root shell with su. Now, proceed with the termination command.
Terminating a process involves sending a termination message to the specified process. There are various types of termination messages, such as:
SIGKILL is a highly effective method for terminating a process. It consistently terminates a process abruptly, resulting in a fatal error. It is expected that SIGKILL will always work. If it fails to do so, then the operating system has encountered a failure.
SIGTERM is used to terminate a process, but unlike SIGKILL, it can be blocked or handled in some cases. It’s a more delicate method of ending a process.
In most cases, SIGKILL is the most efficient and powerful way to end a process.
Below are the most commonly used commands for terminating a process, along with the steps for using each command.
Terminate all processes
The killall command terminates processes based on their name. It sends a SIGTERM signal by default. The killall command is capable of terminating multiple processes simultaneously. Here is the syntax:
killall [process]
The killall command has multiple options available:
-e. Find an exact match for the process name.
-I. Ignore the case when trying to find the process name.
-i. Ask for additional confirmation when killing the process.
-u. Only kill processes owned by a specific user.
-v. Report back on whether the process has been successfully killed.
Here’s an example that demonstrates how to use the -i option with killall to prompt for confirmation before terminating a process:
Not only can the killall command terminate processes by name, but it also has the capability to terminate processes based on their age. Here are the commands you can use:
-o: Use the “-o” flag along with a specified duration to terminate any processes that have been running for longer than the specified time.
-y: Use the “-y” flag in conjunction with a specified duration to terminate any processes that have been running for a shorter period of time.
The command killall -o 15m terminates processes that have been running for more than 15 minutes, whereas the killall -y 15m command terminates processes that have been active for less than 15 minutes.
The pkill command
The pkill command functions similarly to the pgrep command, as it terminates a process by its name, along with other relevant criteria. Typically, pkill will send the SIGTERM signal as its default behaviour. Here is the syntax:
pkill [options] [pattern]
Here are some options for pkill:
-n. Only kill the newest of the processes that are discovered.
-o. Only kill the oldest of the processes that are discovered.
-u. Only kill the processes owned by the specified user.
-x. Only kill the processes that match the pattern exactly.
-signal. Send a specific signal to the process, rather than SIGTERM.
Here’s an example that shows how to terminate the most recent process initiated by the user bosko:
pkill -n -u bosko
kill Command
The kill command is used to terminate processes by their process ID. Here is the syntax:
kill [process ID]
The kill command is used to terminate a single process by specifying its process ID. It sends a signal to terminate a process. It patiently awaits the program to execute its shutdown routine.
You can specify a signal other than SIGTERM using the -signal option.
Executing the kill -9 command in Linux
The “kill -9” command can be quite handy when you need to forcefully terminate an unresponsive service. Execute it in a manner akin to the standard kill command:
kill -9 [processID]
Alternatively:
kill -SIGKILL [processID]
The kill -9 command is used to send a SIGKILL signal to a service, causing it to shut down abruptly. A non-responsive program fails to acknowledge a kill command, but it promptly shuts down when a kill -9 command is executed. Please exercise caution when using this command, as it bypasses the standard shutdown routine, resulting in the loss of any unsaved data.
xkill command
The xkill command is a unique command that terminates the connection between a server and its clients. Here is the syntax for the xkill command:
xkill [resource]
If a server has opened some undesirable processes, xkill can terminate these processes.
When xkill is executed without specifying a resource, a user-friendly interface is displayed, allowing the user to choose which window to close.
top Command
The top command offers a user-friendly interface to navigate the currently running processes. To initiate the interface, execute the following command:
top
If you need to terminate a particular process, simply press ‘k’ while in the interface and provide the PID of the process you wish to kill.
Important: Discover the technique of employing the nohup command to effectively prevent the interruption of processes by the SIGHUP signal, ensuring their completion even after logging out from the terminal or shell.
In conclusion
Terminating a process in Linux is a crucial skill for effective system administration. Every process in the Linux system is assigned a unique process ID (PID) that is used to identify and reference the process. The Linux kill process utilizes the PID to identify and access a process for termination. There are multiple methods for terminating processes in Linux, including using the SIGKILL and SIGTERM signals. It is important to understand the implications of each method and use them with caution, as terminating a critical system process can lead to system instability or even a crash. Users can effectively manage system resources and ensure optimal performance by understanding Linux’s various methods for identifying and terminating processes.
In this article, we discussed various methods for terminating processes in Linux. Understanding these Linux termination commands is crucial for effective system management and administration.
HAVE A QUESTION OR WANT TO GET A CUSTOM SOLUTION?
CONTACT SALESFAQs
A Linux process is an active instance of an application that is running on the system. Every process in the Linux system is assigned a unique process ID (PID) that is used to identify and reference the process. The purpose of a Linux process is to execute a specific task or set of tasks within the system.
The Linux kill process is a command used to terminate a running process in the Linux system. It works by sending a signal to the process, instructing it to terminate. The signal is identified by a unique number or name, such as SIGKILL or SIGTERM, and is used to specify the type of termination requested.
There are several reasons why it may be necessary to terminate a process in Linux, such as when a process becomes unresponsive or starts consuming excessive system resources. Terminating a process can help to free up system resources, improve system performance, and resolve issues caused by unresponsive or malfunctioning processes.
In order to terminate a process in Linux, a user must have the appropriate permissions. This typically includes the ability to access and terminate processes that are linked to the applications they initiated, as well as the ability to access and terminate system processes if they have root or administrative privileges.
There are several methods for identifying the process ID or process name in Linux, including using the ps command, the pgrep or pidof command, and the top command. These commands allow you to view a list of active processes, along with their PIDs, and search for specific processes based on their name or other criteria.
There are several methods for terminating a process in Linux, including using the kill command with the PID, using the SIGKILL or SIGTERM signals, and using the killall or pkill commands. Each method has its own advantages and disadvantages, and the appropriate method will depend on the specific situation and the type of process being terminated.
SIGKILL and SIGTERM are both signals used to terminate a process in Linux, but they differ in how they function. SIGKILL is a more forceful signal that immediately terminates the process, while SIGTERM is a more graceful signal that allows the process to clean up and terminate itself in an orderly manner.
Terminating a process in Linux can be safe if done correctly, but it is important to understand the implications of terminating a process and to use caution when doing so. Terminating a critical system process can cause system instability or even a crash, so it is important to only terminate processes that are safe to do so.
There are several methods for preventing a process from being terminated in Linux, such as using the nohup command or running the process as a background process. These methods can help to ensure that a process continues to run even if the terminal or shell is closed, or if the process is disconnected from the terminal.
If a process cannot be terminated using the standard methods, it may be necessary to use more forceful methods, such as using the SIGKILL signal or rebooting the system. However, it is important to understand the implications of these methods and to use them with caution, as they can cause data loss or other issues if not used correctly.