Are you tired of unresponsive programs, resource-hogging applications, or rogue processes slowing your Linux system down? While knowing how to kill a process on Linux from the command line is a fundamental skill for any Linux user, the story doesn’t end with a simple kill command.

What if you can’t find the process ID (PID)?

What’s the difference between a “job” and a “process” – and how do you manage them?

If you’ve ever found yourself searching for answers to these follow-up questions, you’re in the right place. This article will move past the basic ps aux | grep command and discuss more efficient tools like pgrep and pidof to find exactly what you’re looking for.

We will learn how to manage foreground and background jobs – an essential skill for multitasking in the terminal.

We’ll also tackle common points of confusion, such as the difference between jobs and processes, and how to find resource-hungry processes without relying on top.

But before we kill processes, let’s first refresh our knowledge of what processes are!

What is a Process in Linux?

In Linux, a process is an instance of a running program. Each process has its own memory space, system resources, and a unique Process ID (PID) that the kernel assigns.

Processes can be applications, system services, or background tasks essential for the operating system’s functionality.

Suggested read: Introduction to Bash For Loops: A Beginner’s Guide 

What is Meant by Killing a Process in Linux?

Killing a process in Linux means terminating or stopping a program forcefully. This action is often necessary when a process becomes unresponsive, consumes too many system resources, or needs to be stopped for maintenance or security reasons.

Linux provides several methods to kill processes, ranging from graceful termination signals to forceful stops.

When you kill a process, you’re essentially sending a signal to that process, instructing it to terminate. The most common signal used for this purpose is SIGTERM (signal 15), which allows the process to perform cleanup operations before exiting.

In cases where a process doesn’t respond to SIGTERM, users can employ stronger signals such as SIGKILL (signal 9), which forces immediate termination without allowing for cleanup.

Suggested read: How MailHog Can Transform Your Local Email Testing Process 

Reasons to Kill or Terminate a Process on Linux

There are various scenarios where killing a process becomes necessary:

  • Unresponsive Applications: When a program freezes or becomes unresponsive, terminating it can free up system resources and allow for a restart.
  • Resource Management: Processes consuming excessive CPU or memory can be killed to maintain system stability and performance.
  • Security Concerns: Suspicious or potentially malicious processes should be terminated to prevent security breaches.
  • System Maintenance: During system updates or reconfigurations, certain processes may need to be stopped.
  • Debugging: Developers often need to terminate processes during software testing and debugging.
  • Freeing Up Ports: Killing a process can release network ports that are being held, allowing other applications to use them.
  • Clearing File Locks: Terminating a process can release file locks, enabling access to previously locked files or directories.
  • Stopping Runaway Processes: Accidental infinite loops or other programming errors can create runaway processes that need to be stopped.

Suggested read: What is Docker and How Does it Work 

How to Find a Process ID in Linux

Before you can stop a process, you need to find it, but multiple processes with similar names can run simultaneously. In Linux, every process running on your system has a unique Process ID (PID).

While many users are already familiar with the ps aux | grep command, there are often faster and more precise tools for the job.

Let’s explore the best ways to locate processes.

Using the ps command in Linux

The ps (process status) command gives you a snapshot of the currently running processes. It has many options, but a few combinations are incredibly useful.

  1. ps aux: This is one of the most common commands for tracking processes, as it shows all processes running for all users. You should use ps aux when you need to identify which user owns a process and how much CPU or memory it’s using.

    The ‘aux’ in this case comprises the following three parameters:
    • a = show processes for all users
    • u = display the process’s user/owner
    • x = also shows processes not attached to a terminal

Example: To find the process ID of the RunCloud agent, you could type:

ps aux | grep runcloud

The second column in the output is the PID you need. You’ll notice that the output presented here is quite complex because of the number of columns, and it can be hard to locate what you need.

Let’s look at some of the better alternatives below, which provide us with the Process ID without any other complex information.

  1. ps -ef: This command is similar to ps aux and shows every process on the system in a different format. You should use this command if you also need to find a process’s Parent Process ID (PPID).
    • -e = select every process
    • -f = display full-format listing

Example:

ps -ef | grep runcloud

This command returns cleaner output; the process ID is in the second column. However, some users might still find it hard to read. Let’s look at some of the better options below, which produce even simpler output in human-readable format.

  1. ps -C <process_name>: In the previous commands, the ps utility returned a very long list of processes, and we used a different tool (grep) to extract only what we needed and discard everything else. This command provides a much cleaner way to find a process by its name without needing grep.
    • -C = select by the command name

Example:

ps -C runcloud

This will list only the processes named “runcloud” with a clean output showing the PID and how long this process has been running. This command displays the information in a presentable format that is easy to read. However, it can still be cumbersome to extract the process ID of a particular process in an automated manner.

Using the pgrep Command in Linux

The pgrep (process grep) command is a modern and efficient tool for finding PIDs. It’s faster and less cumbersome than piping ps to grep. If you want to learn more about Linux pipes, we recommend reading our recent blog post on pipes vs xargs.

  1. Basic pgrep: Simply type pgrep followed by the process name.
pgrep runcloud

This command will return only the PIDs of the sshd process, nothing more.

  1. pgrep -l: If you want to see the process name alongside the PID, then you can use the -l (list-name) option
pgrep -l runcloud
  1. pgrep -u <username>: If you need to find all processes run by a specific user, you can use the -u (user) option.
pgrep -l -u root

This will list the PID and name of every process owned by the user “root.”

Using the pidof Command in Linux

All the commands we discussed have returned some extra information. If you just want to see the process ID and nothing else, you can use the pidof command with the process name. This command is simple, fast, and doesn’t require data processing to extract the necessary information. This makes it extremely easy to integrate into bash scripts.

Example:

pidof runcloud

In the above example, if any process named ‘runcloud’ is running, this command will output its PID. 

Using the top Command to find the Process ID

The top command provides a real-time, interactive view of your system’s running processes. Just type top to launch it, and you’ll see a constantly updated list of processes, sorted by CPU usage by default. The PID is in the first column, and the command is in the last column. Once you have found what you are looking for, press the ‘q’ key to exit.

The above example shows that the ‘runcloud’ process has the process ID 670.

Managing Background and Foreground Tasks in Linux

When you run a command in your terminal, it typically runs in the foreground. This means it occupies your terminal, and you must wait for it to finish before entering another command. But what if you need to run a time-consuming task and continue using your terminal?

This can be done using the built-in job control functionality. A job is a wrapper around one or more processes, allowing you to manage them within your current terminal session.

Sending a Job to the Background in Linux Shell

You can start a process directly in the background by adding an ampersand (&) at the end of the command.

Example: Let’s say you’re running a script that takes a long time.

./my-long-script.sh &

The script will start, and the terminal will immediately return you to the command prompt. You’ll see output similar to [1] 12345, where [1] is the job ID and 12345 is the PID.

The above example shows that the ls command was launched in the background and assigned the PID 241370.

Viewing and Managing Jobs with jobs

It is easy to forget once you have sent a job to the background. The jobs command lists all jobs associated with your current terminal session.

jobs -l

The -l option also shows the PID, which is very useful.

The above example shows that the current terminal session has two jobs running in the background.

Bringing Jobs to the Foreground and Background

  • fg (Foreground): If you need to interact with a background job again, you can bring it to the foreground with the fg command followed by its Job ID.
fg %1

This command brings job ID 1 back to the foreground and makes it the active process in your terminal.

  • bg (Background): If you’ve stopped a foreground process (using Ctrl+Z), you can send it to the background to continue running with bg.
bg %1

How to Kill a Process in Linux

There are several ways to kill a process in Linux, but the first step is always to identify the process you want to kill. Once you identify the process, you can choose the following method based on your needs:

How to Kill a Process Using the kill Command With a PID

  1. Using the kill command: Once you have the PID, you can use the kill command to terminate the process:
kill PID

Replace “PID” with the actual number you found. For instance:

kill 1234
kill linux process

This sends a SIGTERM signal, asking the process to shut down gracefully.

  1. Forceful termination: If the process doesn’t respond to the regular kill command, you can use a stronger signal, SIGKILL (9), which forces immediate termination:
kill -9 PID

Be cautious with SIGKILL as it doesn’t allow the process to clean up, potentially leading to data loss or corruption.

How to Kill Multiple Processes

Sometimes, you need to terminate multiple processes simultaneously. Linux provides efficient ways to do this:

  1. Using kill with multiple PIDs: If you know the PIDs of all processes you want to terminate, you can list them after the kill command:
kill PID1 PID2 PID3

For example:

kill 1234 5678 9101
kill multiple linux process via command line
  1. Using command substitution: A more dynamic approach is to use command substitution with pgrep. This method kills all processes matching a name:
kill $(pgrep process_name)

For instance, to kill all Firefox processes:

kill $(pgrep firefox)

This command first uses pgrep to find all PIDs associated with Firefox, then passes these PIDs to the kill command.

Suggested read: Everything You Need To Know About wp-config.php File 

How to Kill a Process Using the pkill Command

The pkill command simplifies process termination by allowing you to kill processes based on their names rather than PIDs:

  1. Basic usage: To kill a process by name, simply type: pkill process_name
     This will terminate all processes with “process_name” in their names.
  2. Case-insensitive matching: If you need clarification on the exact capitalization of the process name, use the -i option. For instance: pkill -i firefox will match “Firefox”, “firefox”, or any other case variation.

How to Kill a Process Using the killall Command

The killall command is similar to pkill but requires an exact match of the process name:

  1. Basic usage: To kill all processes with an exact name match: killall process_name
  2. Forceful termination: For stubborn processes use the -9 option (equivalent to SIGKILL): killall -9 process_name

Suggested read: Mastering the Echo Command in Linux (with Practical Examples) 

How to Kill Process in Linux by User

Sometimes, you need to terminate all processes owned by a specific user:

  1. Listing user processes: First, you can list all processes for a user: ps -u username
     Replace “username” with the actual username.
  1. Killing user processes: To kill all processes for a user, use pkill with the -u option:
    pkill -u username. For example: pkill -u john
  1. Using killall for user processes: Alternatively, you can use the killall command to kill a particular user’s processes, as shown below.
killall -u username

Suggested read: What are Linux Logs? What Are They & How To Use Them 

How to Kill a Process in Linux by Name

Killing processes by name is often more convenient than using PIDs. There are several ways to do this:

  1. Using pkill: The simplest method is by running the following command:
    pkill process_name. Don’t forget to replace process_name with the name of the process you are trying to kill.
  1. Partial name matching: Use the -f option to match a substring in the process name for more flexible matching. This is useful for processes with long or complex names. The syntax of this command is as follows:
pkill -f "partial_process_name"

For example, pkill -f "firefox" would match any process with “firefox” anywhere in its command line.

How to Kill a Process in Linux with Bash Script

Creating a bash script for process termination can be helpful for repetitive tasks. Follow the steps below to avoid typing the long and complex commands:

  1. Create the script: Use a text editor to create a file named kill_process.sh. For example, you can use the nano editor to create the file using the following command:
    nano kill_process.sh.
  2. Add the script content: After creating the file, paste the following content into it:
#!/bin/bash
process_name=$1
pid=$(pgrep -f "$process_name")
if [ -z "$pid" ]; then
    echo "Process not found."
else
    kill $pid
    echo "Process $process_name (PID: $pid) killed."
fi

Once you add the content, you can save it and exit it from the file editor. This script takes a process name as an argument, finds its PID, and terminates it.

Before you can execute the script, you need to make it executable. Change the file permissions by executing the following command to make it executable:

chmod +x kill_process.sh

After changing the file permissions, you can run the script by simply typing its name in the command line, followed by the name of the process you want to kill. For example:

./kill_process.sh firefox

The above command will attempt to kill a process named “firefox” on your computer.

Linux Process Management: At a Glance

This table provides a quick reference for common commands used to manage processes directly from the command line.

Action / GoalBash Command ExampleWhen to Use It
Kill a process by its PIDkill 1234The standard way to terminate a process when you know its exact PID. This sends a graceful shutdown signal (SIGTERM).
Force-kill a process by PIDkill -9 1234A last resort to forcibly terminate a non-responsive process. This sends a SIGKILL signal that cannot be ignored.
Kill multiple specific processeskill 1234 5678When you have a specific list of PIDs, you need to terminate all at once.
Kill a process by partial namepkill 'node'To conveniently kill a process that matches a name or pattern, without needing to find the PID first.
Kill all processes with an exact namekillall 'firefox'To terminate all instances of a specific program (e.g., all open Firefox windows). It’s safer than pkill if other processes have similar names.
Kill all processes owned by a userpkill -u 'Alex'To terminate all processes running under a specific user account, often for administrative or security reasons.

Wrapping Up

In this post, we’ve explored various methods for killing processes in Linux from the command line. From using the kill command with a process ID to tools such as pkill and killall, you should now understand how to terminate unwanted or misbehaving processes on your Linux system.

While a good understanding of the Linux command line helps manage your website, it is not a requirement.

With the help of a hosting platform like RunCloud, you can easily manage your Linux server without getting bogged down in the technical details.

RunCloud provides a user-friendly interface that simplifies server management, allowing you to focus on building and growing your online presence.

Whether you’re a seasoned Linux user or just getting started, RunCloud makes it easy to deploy, monitor, and handle mundane tasks like performing regular backups.

Ready to take your website to the next level? Sign up for RunCloud today and let us handle the Linux management so you can spend more time on what matters most – your business.

FAQs on Killing Linux Processes

How do you kill unnecessary processes in Linux?

To kill unnecessary processes in Linux, you can use commands such as kill, pkill, or killall to terminate the unwanted processes based on their process ID (PID) or name.

What command can you use to kill a process?

The kill -9 PID command, which sends the SIGKILL signal, can be used to forcefully terminate a process that is not responding to a regular termination signal.

How do you find the killed process in Linux?

To find a killed process in Linux, you can use the ps command to list all running processes or the pgrep command to search for processes by name.

How do I gracefully shut down a process in Linux?

To gracefully shut down a process in Linux, you can use the kill command without any signal options, which will send the SIGTERM signal and allow the process to perform cleanup operations before exiting.

How do you end a process by keystroke?

In Linux, you can use the Ctrl+C keyboard shortcut to interrupt and terminate the currently running foreground process.

How do you abort a run in Linux?

To abort a running process in Linux, you can use the Ctrl+C keyboard shortcut. This will send the SIGINT signal and interrupt the process’s execution.

Which key is used to cancel a process?

The Ctrl+C keyboard shortcut is commonly used to cancel or interrupt the currently running process in Linux.

How do I kill a high CPU process in Linux?

In Linux, you can use the top command to identify a process that consumes a large amount of CPU resources and then use the kill command to terminate it.

What’s the difference between a “job” and a “process” in Linux

A process is any program that is currently running on the operating system. The Linux kernel manages all processes.
A job is a shell-level concept that manages one or more processes within a single terminal session. Think of it as a label for a task you’re running. You can move a job to the background or foreground, but the kernel still manages it as a process.

How can I see processes in a tree-like view?

It’s useful to see which processes were started by other processes (parent-child relationships). The pstree command is perfect for this, as it gives you a visual map of everything running on your system, which can be very helpful for troubleshooting.

Author