How to Kill a Process in Linux Using ps, pgrep, pkill and More

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

The Linux operating system has a process manager that allows you to kill running processes with ease. This article will teach how to use the ps command and more commands such as pgrep, pkill, and others in order to terminate a process on your machine.

“Kill process” is a command-line tool that allows users to kill a process in Linux. It can be used with ps, pgrep, pkill and more. Read more in detail here: kill process.

How to Kill a Process in Linux Using ps, pgrep, pkill and More

A rookie system administrator (sysadmin) may find Managing Linux Processes intimidating at first. However, with a little explanation and example, you’ll learn how to use command-line tools like ps and kill to discover and stop processes in Linux.

On any Linux operating system, you’ll discover how to locate running and errant processes, stop (terminate) running processes, detect background processes, and much more in this tutorial.

You’ll be controlling processes based on process identification numbers (PIDs) from the command line like a Linux pro by the conclusion of this tutorial. Continue reading to discover all there is to know about Linux process management commands!

Prerequisites

This article will take you through the process of Managing Linux Processes. You’ll need the following items if you want to be hands-on:

  • A Linux host, which might be a real or virtual computer.
  • Install PowerShell 7.1.1 on a Linux host to test out the PowerShell examples.
  • A user account that isn’t root. While root access is not necessary, there will be an example where the root user may terminate another user’s ongoing process.

It is entirely up to you which Linux distribution you choose. This post will use Ubuntu 20.04 LTS as its foundation. The commands, on the other hand, are standard in the Linux operating system.

Are you ready to begin? Let’s get started.

Managing Linux Processes

On a Linux system, you may find yourself in a situation where you need to terminate a running process. You may have a bash script that runs on a schedule, for example. A cron job is set up to execute the bash script as another user (Linux process scheduler).

This script may sometimes launch errant processes, causing the script to fail. How do you get rid of such rogue processes without having to reset the system or stop the script?

The commands used to find and terminate or kill processes on a Linux system will be covered in the following sections. Every segment will build on the previous one. You’ll learn how to use the native Linux commands ps, top, pgrep, kill, and pkill.

If you choose not to use Linux native commands, PowerShell equivalent commands Get-Process and Stop-Process are given as a bonus.

Before you can kill a process, you must first find the one you want to kill. Precise targeting is possible because to the unique PID. Continue reading to find out how to find the right processes to terminate.

Displaying Process Information with the ps Command

On a Linux system, the ps command, or Process Status, shows information about active processes. With ps, you may find a running process as well as background processes.

By default, the command ps displays all of the processes for the current user’s effective user identification (EUID). The ps command, as seen below, returns a list of all currently active processes that your user may kill.

Displaying processes for the user bkindle that match the EUID (effective user ID).Displaying processes for the user bkindle that match the EUID (effective user ID).

Obtaining More Process Information Making use of the top Command

What if you want to discover regularly updated information on ongoing processes and system metrics? You may use the top command in such scenario. The top command, unlike ps, refreshes the process information on the screen at a predetermined period.

You’ll see more than just a PID when you use the top command. CPU and RAM percentages, Nice values (CPU scheduling process priority), and the Linux kernel’s process priority (PR) are all included in the process information.

The top command is frequently the key tool for controlling essential system resources and operations for a rookie or seasoned administrator. The top command output, as seen below, includes more process information than the ps command, as well as general system metrics.

The top command on Ubuntu Desktop 20.04 LTS showing all running PID's.On Ubuntu Desktop 20.04 LTS, the top command displays all running PIDs.

htop is a more advanced version of top that contains features like a colorized terminal output.

Let’s imagine you want to look at how much memory or CPU a process is using. In such situation, the p switch on the top may be used, as seen below.

The top command, 2113, only displays information for one PID.The top command, 2113, only displays information for one PID.

Perhaps you need to see all the processes that are currently executing for a given login. The u option in the top command allows you to view just the processes of a certain user. PIDs matching just the user ID bkindle are given in the sample below.

Only processes for the user bkindle are shown by the top command.Only processes for the user bkindle are shown by the top command.

Using the pgrep Command to Find Specific Process PIDs

pgrep was originally a Solaris command that was ported to Linux. pgrep enables versatility in determining the precise process to target by combining the search capabilities of the grep command with the process management of ps.

Pgrep is meant to return simply the PID of the returning processes, unlike ps, which provides additional information. Using the different search criteria provided, pgrep assists in identifying the process. In the example below, pgrep is used to look for any processes that match vmtoolsd.

To get all PIDs connected with vmtoolsd, use pgrep.To get all PIDs connected with vmtoolsd, use pgrep.

Similarly, the u switch may be used to provide a user ID to obtain only processes connected with that user. For the user bkindle and the vmtoolsd process, just a single PID is provided, as seen below.

vmtoolsd pgrep -u bkindle

Getting the PID for the vmtoolsd process that belongs to the user bkindle.Getting the PID for the vmtoolsd process that belongs to the user bkindle.

In Linux, using PowerShell’s Get-Process command, you can find processes.

Traditional Linux commands are fine, but did you know that PowerShell can also handle Linux processes? The Get-Process cmdlet provides process information in the same way as the Linux ps program does. For example, let’s say you want to locate all the processes that are currently executing for the current user, bkindle. Find all PIDs (designated ID by Get-Process) of the user bkindle, as shown below.

Get-Process -IncludeUserName | Where-Object

A rookie system administrator (sysadmin) may find Managing Linux Processes intimidating at first. However, with a little explanation and example, you’ll learn how to use command-line tools like ps and kill to discover and stop processes in Linux.

On any Linux operating system, you’ll discover how to locate running and errant processes, stop (terminate) running processes, detect background processes, and much more in this tutorial.

You’ll be controlling processes based on process identification numbers (PIDs) from the command line like a Linux pro by the conclusion of this tutorial. Continue reading to discover all there is to know about Linux process management commands!

Prerequisites

This article will take you through the process of Managing Linux Processes. You’ll need the following items if you want to be hands-on:

  • A Linux host, which might be a real or virtual computer.
  • Install PowerShell 7.1.1 on a Linux host to test out the PowerShell examples.
  • A user account that isn’t root. While root access is not necessary, there will be an example where the root user may terminate another user’s ongoing process.

It is entirely up to you which Linux distribution you choose. This post will use Ubuntu 20.04 LTS as its foundation. The commands, on the other hand, are standard in the Linux operating system.

Are you ready to begin? Let’s get started.

Managing Linux Processes

On a Linux system, you may find yourself in a situation where you need to terminate a running process. You may have a bash script that runs on a schedule, for example. A cron job is set up to execute the bash script as another user (Linux process scheduler).

This script may sometimes launch errant processes, causing the script to fail. How do you get rid of such rogue processes without having to reset the system or stop the script?

The commands used to find and terminate or kill processes on a Linux system will be covered in the following sections. Every segment will build on the previous one. You’ll learn how to use the native Linux commands ps, top, pgrep, kill, and pkill.

If you choose not to use Linux native commands, PowerShell equivalent commands Get-Process and Stop-Process are given as a bonus.

Before you can kill a process, you must first find the one you want to kill. Precise targeting is possible because to the unique PID. Continue reading to find out how to find the right processes to terminate.

Displaying Process Information with the ps Command

On a Linux system, the ps command, or Process Status, shows information about active processes. With ps, you may find a running process as well as background processes.

By default, the command ps displays all of the processes for the current user’s effective user identification (EUID). The ps command, as seen below, returns a list of all currently active processes that your user may kill.

Displaying processes for the user bkindle that match the EUID (effective user ID).Displaying processes for the user bkindle that match the EUID (effective user ID).

Obtaining More Process Information Making use of the top Command

What if you want to discover regularly updated information on ongoing processes and system metrics? You may use the top command in such scenario. The top command, unlike ps, refreshes the process information on the screen at a predetermined period.

You’ll see more than just a PID when you use the top command. CPU and RAM percentages, Nice values (CPU scheduling process priority), and the Linux kernel’s process priority (PR) are all included in the process information.

The top command is frequently the key tool for controlling essential system resources and operations for a rookie or seasoned administrator. The top command output, as seen below, includes more process information than the ps command, as well as general system metrics.

The top command on Ubuntu Desktop 20.04 LTS showing all running PID's.On Ubuntu Desktop 20.04 LTS, the top command displays all running PIDs.

htop is a more advanced version of top that contains features like a colorized terminal output.

Let’s imagine you want to look at how much memory or CPU a process is using. In such situation, the p switch on the top may be used, as seen below.

The top command, 2113, only displays information for one PID.The top command, 2113, only displays information for one PID.

Perhaps you need to see all the processes that are currently executing for a given login. The u option in the top command allows you to view just the processes of a certain user. PIDs matching just the user ID bkindle are given in the sample below.

Only processes for the user bkindle are shown by the top command.Only processes for the user bkindle are shown by the top command.

Using the pgrep Command to Find Specific Process PIDs

pgrep was originally a Solaris command that was ported to Linux. pgrep enables versatility in determining the precise process to target by combining the search capabilities of the grep command with the process management of ps.

Pgrep is meant to return simply the PID of the returning processes, unlike ps, which provides additional information. Using the different search criteria provided, pgrep assists in identifying the process. In the example below, pgrep is used to look for any processes that match vmtoolsd.

To get all PIDs connected with vmtoolsd, use pgrep.To get all PIDs connected with vmtoolsd, use pgrep.

Similarly, the u switch may be used to provide a user ID to obtain only processes connected with that user. For the user bkindle and the vmtoolsd process, just a single PID is provided, as seen below.

vmtoolsd pgrep -u bkindle

Getting the PID for the vmtoolsd process that belongs to the user bkindle.Getting the PID for the vmtoolsd process that belongs to the user bkindle.

In Linux, using PowerShell’s Get-Process command, you can find processes.

Traditional Linux commands are fine, but did you know that PowerShell can also handle Linux processes? The Get-Process cmdlet provides process information in the same way as the Linux ps program does. For example, let’s say you want to locate all the processes that are currently executing for the current user, bkindle. Find all PIDs (designated ID by Get-Process) of the user bkindle, as shown below.

Get-Process -IncludeUserName | Where-Object {$_.UserName -eq ‘bkindle’} | Select-Object -Property ‘Id’,’UserName’,’ProcessName’

Retrieving Linux PID's with PowerShell's Get-Process cmdlet.Using PowerShell’s Get-Process cmdlet to get Linux PIDs.

Related: Using PowerShell’s Where-Object to Filter Everything

After learning how to find various processes, keep reading to understand how to terminate or kill a process in Linux!

Killing Processes in Linux using Kill Commands

In the previous sections, you learned how to use native Linux and PowerShell commands to get the PID of the process to which you want to send a kill signal. The kill and pkill programs, as well as their related signals and command-line switches, will be explained in the following sections.

Signals and Process Management in Linux

Signals are the means through which Linux communicates with the operating system’s processes. The kill command employs three major signals to terminate processes:

  • 1 (SIGHUP) — Ends interactive applications and forces daemons (background services) to re-read the process’s configuration files.
  • 9 (SIGKILL) – Causes the process to quit without gracefully shutting down.
  • 15 (SIGTERM) — Allows a process to gracefully exit, such as when it closes open files. When invoking the kill command without specifying a number, this is the default signal.

Although this article only covers the three most common signals used in killing procedures, there are a total of 64 signals accessible. To retrieve a list of signals and their associated numbers, use kill -l.

 

A fantastic video on how to decipher process signals.

Learn how to kill a process in the next sections now that you know the three basic signals used to terminate a process.

Terminating a Process using the kill Command

The kill command will be used in the following example. Assume you’re running a PowerShell instance called pwsh, and the PID given to it is 22687. This procedure may be terminated in one of many ways, as described below.

  1. Determine the PID for the process pwsh, which is used in the kill command, using pgrep pwsh.
  2. To gently end the pwsh process, use kill -s TERM 22687. The kill command’s s option indicates that the TERM command translates to the 15 (SIGTERM) signal.
  3. To make that the PID is no longer mentioned, use top, ps, or pgrep.

An example of the aforesaid procedure is shown below, taken from an Ubuntu 20.04 LTS bash terminal.

To end the pwsh process, use kill and related commands.To end the pwsh process, use kill and related commands.

The killall command will terminate all processes based on a name rather than a PID, which may be quite damaging if used incorrectly. If you do decide to use this command, be sure to use the I option to have it ask for confirmation.

pkill: Killing Processes by Name

The kill command only works with process IDs, as you may have seen. As a result, ending a process might become a multi-step procedure. The pkill command, which is part of the procps and procps-ng packages, kills a process by searching for its name.

The pkill command functions as a wrapper for the pgrep program in terms of functionality. Instead of just showing the PIDs in the terminal, the pkill command delivers a specific signal to each process discovered. pkill varies from kill in that it can only utilize a process’s name rather than its PID.

In Ubuntu 20.04 LTS, the pkill command is not accessible by default. The program must be downloaded and installed. Run sudo apt install mcollective-plugins-process -y to do so.

To use pkill to terminate a process, follow these steps:

  1. To make sure the nano program (a command-line text editor) is running, type ps. This is an optional step that serves as a safety check before terminating a process.
  2. To aggressively end the nano program, use pkill -9 nano.

An example of the aforesaid procedure is shown below, taken from an Ubuntu 20.04 LTS bash terminal.

To forcibly end the nano process, use the pkill command.To forcibly end the nano process, use the pkill command.

Using the Top Command to Kill a Process

The k keystroke may be used to terminate a specific process when using the top command to examine ongoing processes. An interactive prompt will appear, asking for the PID of a process to terminate.

Run the top command and hit k as indicated below. Enter the PID you wish to kill and hit the Enter key to terminate it right away.

The k keystroke may be used to indicate a process to terminate using top.The k keystroke may be used to indicate a process to terminate using top.

Next, use the PowerShell Stop-Process command to understand another alternative to native Linux commands!

Terminating a Process using PowerShell’s Stop-Process Command

If native Linux commands aren’t your thing, PowerShell can help! In Linux, you may terminate processes using the Stop-Process cmdlet, just as you do with the kill command. Pretend you’re running a PowerShell pwsh process once again.

  1. Get-Process -ProcessName ‘pwsh’ to get the PID and note the PIDs to terminate.
  2. To end the multiple processes returned, use Stop-Process -Id 29992,30014.
  3. Check that the PIDs 29992 and 30014 are no longer displayed in Get-Process -ProcessName ‘pwsh’, indicating that they have been terminated.

You may also kill one or more processes using Get-Process and the kill method: (Get-Process -Id [PID]). or kill() (Get-Process -Id [PID1,2,3,4]). Kill().

When utilizing the ProcessName argument, be cautious. This option works in the same way as the killall command in that it kills all processes that match the name(s) specified.

An example of the aforesaid procedure is shown below, taken from an Ubuntu 20.04 LTS bash terminal.

Stop-Process is a PowerShell command that may be used to stop processes on Linux.Stop-Process is a PowerShell command that may be used to stop processes on Linux.

Steps to Follow

You should now be familiar with the many methods for finding and terminating processes in Linux! Try killing processes remotely via SSH connections or by killing processes that may be operating in containers as your next challenge.

Setting up SSH in Linux (A Windows Guy in a Linux World)

.UserName -eq ‘bkindle’ | Select-Object -Property ‘Id’,’UserName’,’ProcessName’ | Select-Object -Property ‘Id’,’UserName’,’ProcessName’

Retrieving Linux PID's with PowerShell's Get-Process cmdlet.Using PowerShell’s Get-Process cmdlet to get Linux PIDs.

Related: Using PowerShell’s Where-Object to Filter Everything

After learning how to find various processes, keep reading to understand how to terminate or kill a process in Linux!

Killing Processes in Linux using Kill Commands

In the previous sections, you learned how to use native Linux and PowerShell commands to get the PID of the process to which you want to send a kill signal. The kill and pkill programs, as well as their related signals and command-line switches, will be explained in the following sections.

Signals and Process Management in Linux

Signals are the means through which Linux communicates with the operating system’s processes. The kill command employs three major signals to terminate processes:

  • 1 (SIGHUP) — Ends interactive applications and forces daemons (background services) to re-read the process’s configuration files.
  • 9 (SIGKILL) – Causes the process to quit without gracefully shutting down.
  • 15 (SIGTERM) — Allows a process to gracefully exit, such as when it closes open files. When invoking the kill command without specifying a number, this is the default signal.

Although this article only covers the three most common signals used in killing procedures, there are a total of 64 signals accessible. To retrieve a list of signals and their associated numbers, use kill -l.

 

A fantastic video on how to decipher process signals.

Learn how to kill a process in the next sections now that you know the three basic signals used to terminate a process.

Terminating a Process using the kill Command

The kill command will be used in the following example. Assume you’re running a PowerShell instance called pwsh, and the PID given to it is 22687. This procedure may be terminated in one of many ways, as described below.

  1. Determine the PID for the process pwsh, which is used in the kill command, using pgrep pwsh.
  2. To gently end the pwsh process, use kill -s TERM 22687. The kill command’s s option indicates that the TERM command translates to the 15 (SIGTERM) signal.
  3. To make that the PID is no longer mentioned, use top, ps, or pgrep.

An example of the aforesaid procedure is shown below, taken from an Ubuntu 20.04 LTS bash terminal.

To end the pwsh process, use kill and related commands.To end the pwsh process, use kill and related commands.

The killall command will terminate all processes based on a name rather than a PID, which may be quite damaging if used incorrectly. If you do decide to use this command, be sure to use the I option to have it ask for confirmation.

pkill: Killing Processes by Name

The kill command only works with process IDs, as you may have seen. As a result, ending a process might become a multi-step procedure. The pkill command, which is part of the procps and procps-ng packages, kills a process by searching for its name.

The pkill command functions as a wrapper for the pgrep program in terms of functionality. Instead of just showing the PIDs in the terminal, the pkill command delivers a specific signal to each process discovered. pkill varies from kill in that it can only utilize a process’s name rather than its PID.

In Ubuntu 20.04 LTS, the pkill command is not accessible by default. The program must be downloaded and installed. Run sudo apt install mcollective-plugins-process -y to do so.

To use pkill to terminate a process, follow these steps:

  1. To make sure the nano program (a command-line text editor) is running, type ps. This is an optional step that serves as a safety check before terminating a process.
  2. To aggressively end the nano program, use pkill -9 nano.

An example of the aforesaid procedure is shown below, taken from an Ubuntu 20.04 LTS bash terminal.

To forcibly end the nano process, use the pkill command.To forcibly end the nano process, use the pkill command.

Using the Top Command to Kill a Process

The k keystroke may be used to terminate a specific process when using the top command to examine ongoing processes. An interactive prompt will appear, asking for the PID of a process to terminate.

Run the top command and hit k as indicated below. Enter the PID you wish to kill and hit the Enter key to terminate it right away.

The k keystroke may be used to indicate a process to terminate using top.The k keystroke may be used to indicate a process to terminate using top.

Next, use the PowerShell Stop-Process command to understand another alternative to native Linux commands!

Terminating a Process using PowerShell’s Stop-Process Command

If native Linux commands aren’t your thing, PowerShell can help! In Linux, you may terminate processes using the Stop-Process cmdlet, just as you do with the kill command. Pretend you’re running a PowerShell pwsh process once again.

  1. Get-Process -ProcessName ‘pwsh’ to get the PID and note the PIDs to terminate.
  2. To end the multiple processes returned, use Stop-Process -Id 29992,30014.
  3. Check that the PIDs 29992 and 30014 are no longer displayed in Get-Process -ProcessName ‘pwsh’, indicating that they have been terminated.

You may also kill one or more processes using Get-Process and the kill method: (Get-Process -Id [PID]). or kill() (Get-Process -Id [PID1,2,3,4]). Kill().

When utilizing the ProcessName argument, be cautious. This option works in the same way as the killall command in that it kills all processes that match the name(s) specified.

An example of the aforesaid procedure is shown below, taken from an Ubuntu 20.04 LTS bash terminal.

Stop-Process is a PowerShell command that may be used to stop processes on Linux.Stop-Process is a PowerShell command that may be used to stop processes on Linux.

Steps to Follow

You should now be familiar with the many methods for finding and terminating processes in Linux! Try killing processes remotely via SSH connections or by killing processes that may be operating in containers as your next challenge.

Setting up SSH in Linux (A Windows Guy in a Linux World)

The “pkill vs kill” is a process killing tool that can be used in Linux. It is more powerful than the “kill” command.

Related Tags

  • kill process by pid linux
  • kill process linux
  • ps kill process by id
  • force kill process linux
  • kill process by name