Home Icon Home Resource Centre How To Kill Process In Linux | Ways And Commands To Locate & Kill

How To Kill Process In Linux | Ways And Commands To Locate & Kill

A kill process is a command that is used in computer operating systems to terminate a running process. It is simply a way of stopping a program. Let's explore this in detail.
Shivani Goyal
Schedule Icon 0 min read
How To Kill Process In Linux | Ways And Commands To Locate & Kill
Schedule Icon 0 min read

Table of content: 

  • What Is The Kill Process In Linux?
  • Why Do We Need To Kill A Process?
  • How To Locate And Kill A Process In Linux?
  • Different Ways To Kill A Process In Linux
  • 4 Commands To Kill A Process In Linux
  • Additional Options For Kill Commands In Linux
  • Conclusion
  • Frequently Asked Questions
expand icon

Wondering what is a kill process is in Linux or how to kill a process in this OS? If so, you’re in the right place. In this article, we will explore what it means to kill process in Linux, along with how to locate and terminate processes using various tools and commands.

Let's start with the basics: what exactly is a process?

By definition, a process is a running instance of a program, each with its own unique Process ID (PID). But sometimes, a running process may become unresponsive process or start consuming excessive system resources, making it necessary to kill or terminate the process. This is where the kill process command in Linux OS comes into play.

What Is The Kill Process In Linux?

In general computing, a kill process is a command used in Linux and other Unix-based operating systems to terminate a running process. Simply put, it stops a program or application currently running on a computer, either because it is unresponsive or is causing issues, etc. 

Linux has a number of tools to destroy a rogue/ errant process from the terminal. For instance, every individual process/ program launched on a computer is assigned a unique identifier called a process ID (PID). This PID helps track the program's execution and process activity as well as terminate the process when needed. The kill process command sends a signal to the operating system, which then stops the program associated with the specified PID.

The virtual memory usage is a feature of Linux, that allows every process to have access to the whole computer's physical memory. This enables memory overcommitment, which further facilitates efficient programming. As a result, even if another process has already taken the available memory, each process can swiftly obtain additional memory.

Why Do We Need To Kill A Process?

There are various reasons why a user may need to kill a process using various kill process commands.  Some of these are:

  • Unresponsive Programs: If a program has frozen or become unresponsive, the user may not be able to close it using the standard close button. In this case, the kill process command can be used to terminate the program forcibly.
  • Freeing Up Resources: There are times when unresponsive programs continue running in the background even after they have been closed, thus slowing the computer down. Using the kill process command can help free up system resources (memory space, etc.) being used up by these background processes.

Caution: Use the kill process command carefully. Terminating critical system processes can lead to instability or even crash the system. Only use the kill process command with proper command-line knowledge and when necessary.

How To Locate And Kill A Process In Linux?

To locate a single process in Linux, you can use the command-line tool ps (process status) in combination with the grep command to search for specific process names, partial process names, or keywords. The ps command is just an entry in the process table (zombie process), kept around so that the parent process can be notified of the death of its child.

Steps to Locate a Process in Linux:

Step 1: Open a terminal window.

Step 2: Type the following command to view a list of all running processes:

ps -ef

This will display a list of all running processes along with their PID (process ID) and other information.

Step 3: To find a specific process, you can use the grep command to filter the output. For example, if you want to find a process named "firefox", you can use the following grep command:

ps -ef | grep firefox

This will display all the processes that contain the keyword "firefox" in their name or command line arguments.

Step 4: Once you have identified the current process you want to kill, note down its PID (the second column in the output of the "ps" command).

Step 5: Finally, use the kill command with the PID to terminate the process. For example, if the PID of the process is 1234, you can use the following command:

Finally, use the kill command with the PID to terminate the active process. For example, if the PID of the process is 1234, use the following command:

kill 1234

This will send a SIGTERM signal to processes, instructing it to terminate. If the process does not respond to SIGTERM, you can use the kill -9 PID command to send a SIGKILL signal, which will then forcibly execute the killing of process. For example, in the above case, the kill-9 command will be:

kill -9 1234

Different Ways To Kill A Process In Linux

There are multiple ways to termiante/ kill processes in Linux, depending on the information you have (such as PID or process name) and the level of control you want over the termination.

1. Killing by PID: The simplest method is to kill a process using its PID with the kill command:

kill PID

This sends the default signal (SIGTERM) to terminate the process politely. (Just as discussed above)

2. Killing by Signal Number: Linux uses numeric values to represent different signals. For instance, SIGTERM is represented by 15, and SIGKILL by 9. You can send these signals directly by number:

kill -9 1234

This example sends SIGKILL (9), which forcefully terminates the process.

3. Using the SIG Prefix: Instead of numbers, you can specify signals by their full names with the SIG prefix:

kill -SIGKILL 1234

This method is more descriptive and clarifies the intention, as in -SIGKILL or -SIGTERM.

4. Killing by Command Name or Pattern: If you don’t know the PID, use the killall or pkill commands to target processes by name:

killall firefox

This kills all processes named "firefox." Similarly, pkill can be used to match patterns within the process name.

Now that we know about the different ways to kill a process in Linux, let's look at the various kill command offered by the Unix-based operating systems.

4 Commands To Kill A Process In Linux

Various commands in Linux can be used to kill a process, each with different options and levels of control:

1. The Kill Command

The primary and the most simple command used to kill a process in Linux is the kill command. It is one of the easiest commands to use when you know the exact process name. Linux keeps the status of both the parent and child processes in a detailed process list until the process is permanently deleted.

The basic syntax for the kill command in Linux is:

kill [signal] PID

Here, "signal" is an optional parameter that specifies the type of signal to send to the parent process. If no signal is specified, the default signal sent is SIGTERM (terminate signal).

The "PID" parameter specifies the process ID of the parent process to be killed. By default, every process has a unique PID parameter.

Some commonly used special signals with the "kill" command are:

  • SIGTERM (15): This is the default signal sent by the "kill" command. It asks the parent process to terminate gracefully/ politely.

  • SIGKILL (9): This signal forcibly terminates the parent process without allowing it to perform any cleanup or exit code.

Note: The init process (the master process on boot) has a unique PID and cannot be terminated with the Linux kill command. In other words, the first process that starts when the Linux system is booted is the init process. The init process is the master process and can not be directly killed using the kill command. 

Advantages:

  • The kill command is easy to use and works for most situations.
  • It allows you to send different signals to a process, depending on the situation.

Disadvantages:

  • It may not work for processes that are in a blocked state or unresponsive programs.
  • If forced on an active program, the SIGKILL signal with the kill command may cause data loss or other unexpected behavior.

2. The Killall Command

As the name suggests, the killall command is used to kill all processes in Linux with a single command. The said processes are killed based on their process name or other attributes. It is similar to the "kill" command, but it allows you to kill multiple processes at once based on a pattern match.

The syntax for the killall command is:

killall [options] process_name

Here, "process_name" is the name of the process or processes to be killed. If you wish to kill a process as a different user or if the Linux kill app command fails, you can use the command with sudo as follows:

sudo killall process_name

Some common options used with the killall command are:

  • Option -e: This option stipulates that the process name should match exactly. If this option is not used, killall will match any process name that contains the specified string.

  • Option -i: This option prompts before killing each process.

  • Option -s signal: This option allows you to specify a common signal to send to the background process. The default signal is SIGTERM.

3. The Pkill Command

Another useful tool in Unix-based operating systems, the pkill kills processes based on the process name or other attributes. It is similar to the killall command but provides more flexibility in terms of pattern matching and sending signals.

The syntax for the pkill command is as follows:

pkill [options] process_name

Here, "process_name" is the name of the unwanted process you want to kill or any keyword in its name. For example, if you want to send kill signals to processes whose name contains the string "fire" in them, use the following command:

pkill fire

Some common options used with the pkill command are:

  • Option -f: This option matches the pattern against the entire command line rather than just the process name.

  • Option -u username: This option helps target a specified user processes. That is, it kills the processes belonging to the specified user/ username.

  • Option -n: This option selects the newest (most recently started) processes that match the pattern.

  • Option -s signal: This option allows you to specify a signal to send to the processes. The default signal is SIGTERM.

Advantages:

  • The Pkill command is useful for killing multiple processes that match a certain pattern.
  • It allows you to search for processes based on their names or other attributes.

Disadvantages:

  • It may kill unintended processes that match the specified pattern.
  • It may not work for processes that are in a blocked state or that have become unresponsive.

4. The Pidof Command

This command helps find the process ID (PID) of the running process whose name you specify in the command. (Hence the pidof process– PID of the process).

The syntax for pidof command is:

pidof [OPTIONS] NAME

Heere, the NAME is the process for which you want to find the PID.

Once you have the PID of the process, you can use the kill command to terminate it. You can even use the PID obtained above with the -9 <pid> option to force the process to terminate immediately without any chance to clean up or save data. For example, to kill the process with PID 1234, you can use the following command:

kill -9 4534

This will send the SIGKILL signal to the process with PID 1234, forcing it to terminate immediately.

Take part in the 100-Day Coding Sprint at Unstop and increase your chances of topping the leaderboard now!

Additional Options For Kill Commands In Linux

In addition to the options discussed for specific commands, here are a few other commonly useful flags that apply to kill commands in the Unix-based operating system:

  • -l: Lists all available signal names, which can help if you’re unsure which signal to use. For example

kill -l

  • -a: Ensures killall targets processes across different users, not just your own. For example:

killall -a process_name

  • --signal [number]: Specifies thesignal to send in killall or pkill commands without needing to know the name. For example:

killall --signal 15 process_name

Conclusion

Linux is a popular open-source operating system known for its flexibility, scalability, and stability. One powerful feature it offers is comprehensive process management, including the ability to kill or terminate processes when needed. Killing a process in Linux can be essential in cases where a process becomes unresponsive, consumes excessive system resources, or is simply no longer needed. With various commands and options, Linux users have several ways to efficiently manage and control processes, ensuring smooth system performance and resource optimization.

Frequently Asked Questions

Q1. What does it mean to “kill” a process in Linux?

Killing a process in Linux means terminating it, or stopping its execution. This can be useful if a process has become unresponsive, is using too many system resources, or simply needs to be stopped for other reasons.

Q2. How can I find the process ID (PID) of a running program?

You can use commands like ps, pidof, or pgrep to locate the PID of a process. For example, ps -ef lists all running processes with their PIDs, and pidof process_name gives the PID of a specific process by name.

Q3. What’s the difference between SIGTERM and SIGKILL signals?

SIGTERM (signal 15) requests a process to terminate gracefully, allowing it to complete any cleanup operations before stopping. SIGKILL (signal 9), however, forcibly terminates a process immediately, without any cleanup, which can risk data loss.

Q4. When should I use the killall or pkill commands instead of kill?

Use killall or pkill when you want to target processes by name or pattern rather than by PID. The killall command stops all processes matching the name, while pkill provides more flexibility, such as matching partial names or restricting to specific users.

Q5. Can killing a critical system process crash my Linux system?

Yes, terminating essential system processes like init or other core services can destabilize or even crash your system. Use kill commands carefully, especially with system-level processes, to avoid unintended consequences.

Q6. Is there any way to undo a kill command after it’s sent?

Once a process is terminated, it cannot be undone. If you terminate a critical process, you may need to restart the application or, in some cases, reboot the system.

Q7. How can I check which signals are available on my system?

You can list all available signals by using the kill -l command in the terminal. This will display signal names and corresponding numbers, helping you choose the appropriate one. You can then use it with the kill command in Linux systems to terminate a process.

You might also be interested in reading the following:

  1. Introduction To Linux Shell, Features, Types, Scripting & More
  2. Linux File Permissions Simplified In This Complete Guide
  3. Linux Zip Command: Examples, Compression & Zip File Utility
  4. How To Create A File In Linux: Touch, Cat, Echo, Printf Command
Edited by
Shivani Goyal
Manager, Content

An economics graduate with a passion for storytelling, I thrive on crafting content that blends creativity with technical insight. At Unstop, I create in-depth, SEO-driven content that simplifies complex tech topics and covers a wide array of subjects, all designed to inform, engage, and inspire our readers. My goal is to empower others to truly #BeUnstoppable through content that resonates. When I’m not writing, you’ll find me immersed in art, food, or lost in a good book—constantly drawing inspiration from the world around me.

Tags:
Computer Science Engineering

Comments

Add comment
avatar

Tea 1 year ago

Thanks a million
Powered By Unstop Logo
Best Viewed in Chrome, Opera, Mozilla, EDGE & Safari. Copyright © 2024 FLIVE Consulting Pvt Ltd - All rights reserved.