How to Kill a Process in Linux?

How to Kill a Process in Linux_ Commands to Terminate

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 how to list processes to get the associated process IDs. Next, we’ll go into the details of the commands you use to kill a process in Linux.

How to Find Process ID in Linux

Every process, whether started by the users or system components, has a numeric ID (called PID) that uniquely identifies it throughout the system. 

The interesting thing about processes and PID is that all instances of the same process are assigned a unique PID. As a result, you can distinguish between the instances and choose to kill a specific instance rather than terminating all instances of the process. This is very useful in situations where you are running parallel instances of a utility such as the package manager. 

Processes on your Linux system can be broadly divided into two categories – Foreground processes and Background processes. We will now discuss how to find the PID of a foreground or background processes.

Find the Process ID of Foreground Processes

Linux offers several options to view the information (including the PID) of the foreground processes. We will mention three commands that can help you identify the right PID you can use to kill a process. 

Method #1: Find PID with the ps Command

The ps command is a very flexible way of discovering the processes running on the system. When used without any parameters, it simply displays the foreground processes for the currently logged-in user. This command is also used for finding out the current PID of the shell. 

The command syntax is as follows:

# ps 

ps

However, you can get a more comprehensive view of the processes by adding the a, u, and x flags to the command. The output of the command shows the specific command associated with the PID. The command in this context is:

# ps aux

ps aux

Method #2: Find PID with the pidof Command

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> <process name>

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.

For instance, consider the following command to get the PID associated with the cat command on our test system:

# pidof cat

pidof cat

Method #3: Find PID Associated with a User with pgrep

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 -l -u root 'a*'

 pgrep -l -u root 'a*'

Find the Process ID of Background Processes

Before you can kill a background process, you need to know the correct PID. You can use the ps and pidof commands to find the PID for background processes.  

Method #1: Find Background PID with the ps Command

You can use a slightly modified version of the ps command to list the PID of processes matching a specific process name. Noet that this command syntax pipes the output of the ps command in grep to filter the output:

# ps -e | grep -i 'pattern'

Method #2: Use the top Utility

The top utility offers a detailed view of the system processes. By simply calling the utility at the command line, you can get a description of all active processes, including the background processes. 

# top 

top

Press ‘q’ to exit the utility.

How to Kill Processes in Linux

Now that you know how to find the PID of a process, we can move to the next half of this tutorial. Here we will discuss two methods of killing a process.

Method #1: Use the top Command to Kill Linux Processes

You know how to use the top utility to list processes so that you know the PID of the process you wish to kill. This command also displays the IDs of users and processes as well as information about each process’s memory and CPU utilization.In addition, you can kill the process from within the utility. For this, follow these steps:

  • Launch the utility:

    # top
  • Use the UP or DOWN arrows to move the process you wish to kill to the top of the listing. For instance, we wish to kill the process associated with the top utility
  • Press k to send the kill select the kill signal to the process. The utility will ask you to enter the PID to terminate the process.

kill signal

 

  • The utility will send the SIGTERM/15 signal to the process, effectively terminating it.

sigterm

Method #2: Use the kill Command to Terminate Processes

The kill command is very versatile and comes with several related utilities to cater to specific scenarios. 

Before going into the details of the kill command and the related utilities, it is important to understand how kill command works and the idea of signals. 

What is the kill Command?

When you enter the kill command and include a PID, the command sends a default TERM/15 signal to the process. This signal gives the process enough time to save any unsaved data and ensure a graceful exit. However, you can send other signals to kill processes in specific conditions. Here is a brief overview of the most common signals you can use with the command:

  • SIGHUP/1: This signal hangs up or reloads the process. This behavior is controlled by the terminal configuration.
  • SIGINT/2: This sends a keyboard interrupt to the process. In most shells, this is similar to the ^C.
  • SIGKILL/9: This sends an immediate kill signal to the process. 
  • SIGTERM/15: This sends a graceful exit signal to the process.

You can run the kill -l command for a complete list of all signals. 

 kill -l

You can kill multiple processes with a single command by adding the PID to the kill command. We recommend extreme caution in entering the PID because the system will not vet the list of PID and send the SIGTERM or SIGKILL signal. 

The syntax of the command is as follows:

# kill <PID1> <PID2> …

The kill system calls are very flexible and cater to all important scenarios where you need to interfere with the normal course of a process’s execution. 

Kill 9 (SIGKILL): Kill a Process Immediately

You can immediately kill a process by sending it the kill 9 (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.

Consider the following command to kill the xed process which has the PID of 29046:

# kill -9 29046

 

This sends a kill system call to the process. We used the ps command to check if this kill 9 call was successful

# ps 29046

ps 29046

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.

Another use case of the killall command is to kill multiple processes by mentioning their names in the command. The command syntax is as follows:

# killall <process_name_1> <process_name_2> <process_name_3>

You can use this command syntax to kill the processes with a similar name. In this context, we recommend using a wildcard to include all related processes:

# killall <process_name>*

As with most kill system calls, you can specify a signal in a killall command. By default, the killall command sends a SIGTERM signal to the processes. You can modify this with this command syntax: 

# killall -s SIGKILL <process_name_1> <process_name_2> <process_name_3>

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 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>

How to Stop a Process in Linux

Instead of termination, you can stop process by sending the relevant SIGSTOP signal. Note that this signal acts as a pause button for the process. 

Consider the following command that sends a SIGSTOP signal to the xed process with the PID of 30866. Now, we will send a SIGSTOP signal to this process with the following command:

# kill -s SIGSTOP 30866

Now, check the status of the process with the following ps command:

# ps 30866

We can see that the STAT column of the output shows T, indicating that the process is stopped. 

ps 30866

You can use this process to kill a process in Unix-like environments that support the standard kill command implementation. This is a useful idea when you wish to temporarily block the execution of a process. 

This is a common operational requirement when you run a process that takes a long time to complete. In other cases, a process often has to wait for other processes to finish so that the output of those processes can be used as input for the target process. 

Killing Shell Processes

An important use case of killing processes in Linux is killing the current shell. The shell can get stuck during an operation or may become unresponsive while waiting for a response. In these cases, the easiest option is to kill the shell and launch a new terminal. 

This seemingly impossible action is possible because in Linux , the shell is just another process running within the system.

The first step of this process is to know the PID of your current shell. For this, go to the shell and get the process ID of the current process (the shell). The command to get this PID is as follows:

# echo $$

Now, you can use the PID of the shell process with the kill -9 command to kill the shell.

 echo $$

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.
  • Remember that processes paused using kill %2 can still take up system resources. You should carefully consider the benefits of pausing a process against the requirements for system resources to ensure system stability. 

Conclusion

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.

FAQs

Q. How do you kill a process in Unix/Linux?

You can kill a process in a Unix-based OS environment by initiating a kill system call. Depending upon the situation, you can use the SIGTERM/15 or SIGKILL/9 kill system calls. 

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.

Narendra

At Redswitches, Narendra works as a DC engineer full-time. He is in charge of Redswitches Asia DC, which is based in India. His area of expertise is server hardware. Rack and DC Operation resolving all OS-related concerns.

Related articles

Latest articles