- Adam the Automator
- How to Kill a Process in Linux Using ps, pgrep, pkill and More
- Bill Kindle
- Prerequisites
- Managing Processes in Linux
- Tools for Locating Processes
- Using the ps Command to Display Process Information
- Finding Expanded Process Information Using the top Command
- Locating Specific Process PIDs with the pgrep Command
- Finding Processes with PowerShell’s Get-Process in Linux
- Killing Processes Using the Kill Commands in Linux
- Linux Signals and Process Management
- Using the kill Command to Terminate a Process
- Killing Processes by Name with pkill
- Killing a Process with the Top Command
- Using the PowerShell Stop-Process Command to Terminate a Process
- Next Steps
- Use killall and kill Commands to Stop Processes on Linux
- Usage
- How to Use killall
- How to Use kill
- System Signals
- Find Running Processes
- Verify Process Termination
Adam the Automator
How to Kill a Process in Linux Using ps, pgrep, pkill and More
Bill Kindle
Read more posts by this author.
Managing processes in Linux can appear daunting to a new system administrator (sysadmin) at first. But with a little explanation and demonstration, you’ll learn how to find and kill processes in Linux is just a matter of using command-line tools such as ps and kill .
In this article, you will learn commands for locating running and errant processes, killing (terminating) running processes, finding background processes, and much more on any Linux operating system.
By the end of the article, you will be managing processes based on process identification numbers (PIDs) from the command line like a Linux pro. Keep reading to learn all about process management commands in Linux!
Table of Contents
Prerequisites
This article is going to be a walk-through of how to manage processes in Linux. If you want to be hands-on, you will need the following:
- A Linux host, which can be a virtual machine or a physical machine.
- To try the PowerShell examples, install PowerShell 7.1.1 on a Linux host.
- A non-root user account. While root access is not required, there will be an example where the root user can kill the running process of another user.
The distribution of Linux, you choose to use, is up to you. This article will be based on Ubuntu 20.04 LTS. However, the commands are standard for the Linux operating system.
Ready to get started? Good, let’s dive in.
Managing Processes in Linux
On occasion, you may run into a scenario where you need to stop a running process on a Linux system. As an example, you may have a bash script that runs on a schedule. That bash script is configured to run as another user via a cron job (Linux process scheduler).
Sometimes, this script spawns errant processes that can make the script fail. How do you kill those errant processes without rebooting the system or stopping the script?
In the next sections, you will walk through commands used to locate and terminate or kill processes on a Linux system. Each section will build on the last. You will learn about ps , top , pgrep , kill , and pkill commands all native to Linux.
As a bonus, PowerShell equivalent commands Get-Process and Stop-Process are included if Linux native commands are not your style.
Tools for Locating Processes
Before killing a process you must locate the correct process to target. The unique PID allows for precise targeting. Read on to learn how to locate the correct processes for termination.
Using the ps Command to Display Process Information
The Process Status or ps command displays information related to active processes on a Linux system. You can locate a running process and even background processes with ps .
The command ps will, by default, display all of the processes for the effective user identification (EUID) of the current user. Running the ps command, as shown below, will return every running process that your user can terminate.
Finding Expanded Process Information Using the top Command
What if you want to find constantly refreshing information on running processes and system metrics, in that case, you can use the top command. Unlike ps , the top command updates the process information on the screen over a set interval.
When you run the top command, you will see more than just a PID. Included with process details are CPU and memory percentages, Nice values (CPU scheduling process priority), and the process priority (PR) set by the Linux kernel.
For a new or experienced sysadmin, the top command is often the primary tool for managing vital system resources and processes. As shown below, the top command output displays more process information than the ps command including overall system metrics.
htop is an enhanced variant of top which includes various improvements, such as a colorized terminal output.
Let’s say you’d like to inspect the memory or CPU resources a process is consuming. In that case, you can use the p switch of top as shown below.
Perhaps you need to display all running processes for a specific username. The top command offers the u switch to display only a specific user’s processes. As seen in the example below, PIDs matching only the user ID bkindle are shown.
Locating Specific Process PIDs with the pgrep Command
Originally a Solaris command, pgrep was ported for use with Linux. Combining the search power of the grep command and the process management of ps , pgrep offers flexibility in finding the exact process to target.
Although ps displays more information, pgrep is designed to return only the PID of the returned processes. pgrep helps with locating the process using the many available search parameters. In the following example, pgrep is shown searching for any process matching vmtoolsd .
Similarly, specify a user ID with the u switch to retrieve only processes associated with that specific user. As shown below, only a single PID is returned for the user bkindle and the vmtoolsd process.
Finding Processes with PowerShell’s Get-Process in Linux
Typical Linux commands work great, but did you know PowerShell is able to manage Linux processes too? The Get-Process cmdlet works similar to the Linux ps command and returns process information. For example, perhaps you need to find all running processes by the current user, bkindle . As shown below, find all PIDs (labeled ID by Get-Process ) of the user bkindle .
Having learned how to locate different processes, read on to discover all the ways to terminate or kill a process in Linux!
Killing Processes Using the Kill Commands in Linux
In previous sections, you learned how to locate the PID of the process you want to send a kill signal to using native Linux and PowerShell commands. The next sections will explain how to use the kill and pkill programs, its associated signals, and command-line switches.
Linux Signals and Process Management
Signals are the method that Linux uses to communicate with processes running in the operating system. The three primary signals that the kill command uses to terminate processes are:
- 1 (SIGHUP) – Terminates interactive programs and causes daemons (background services) to re-read the configuration files the process uses.
- 9 (SIGKILL) – Forces the process to exit without performing graceful shutdown tasks.
- 15 (SIGTERM) – Allows a process to terminate gracefully, such as closing open files when finished. This is the default signal used when no number is specified when using the kill command.
While this article only focuses on the three main signals used for killing processes, there are a total of 64 available signals. Use kill -l to get the list of signals and their corresponding number.
Now that you understand the three main signals used in terminating a process, learn how to kill a process in the next sections.
Using the kill Command to Terminate a Process
In this next example, you will use the kill command. Pretend for a moment that you are running a PowerShell instance, named pwsh , and the PID assigned is 22687. One way to terminate this process is shown below.
- Use pgrep pwsh to determine the PID for the process pwsh , used in the kill command.
- Use kill -s TERM 22687 to terminate the pwsh process gracefully. The TERM command maps to the 15 (SIGTERM) signal and indicated using the s parameter of the kill command.
- Use your choice of top , ps , or pgrep to verify the PID is no longer listed.
Shown below is an example of the above process from an Ubuntu 20.04 LTS bash terminal.
The killall command will terminate all processes based on a name instead of PID, which can make this command pretty destructive if used carelessly. If you choose to use this command, however, use the i option to cause the command to ask for confirmation.
Killing Processes by Name with pkill
You may have noticed that the kill command only works with process IDs. This can make terminating a process a complicated multi-step process. The pkill command, included in the procps and procps-ng packages, terminates a process based on a searched name.
Functionally, the pkill command acts as a wrapper around the pgrep program. The pkill command sends a specified signal to each process found instead of only listing the PIDs in the terminal. pkill differs from kill in that pkill can only use the name of a process, not the PID.
The pkill command is not available, by default, in Ubuntu 20.04 LTS. You will have to download and install the application. To do so, run sudo apt install mcollective-plugins-process -y .
To kill a process using pkill , perform the following steps:
- Use ps to verify the nano application (a command-line text editor) is currently running. This step is optional, but a safety check before killing a process.
- Use pkill -9 nano to forcefully terminate the nano application.
Shown below is an example of the above process from an Ubuntu 20.04 LTS bash terminal.
Killing a Process with the Top Command
When you run the top command to view running processes, you can kill a given process with the k keystroke. You’ll see an interactive prompt asking for the PID of a process to kill.
As shown below, run the top command and press k. Enter the PID that you want to kill, and press the Enter key to immediately terminate the PID.
Next, learn one more alternative to native Linux commands using the PowerShell Stop-Process command!
Using the PowerShell Stop-Process Command to Terminate a Process
If Linux native commands aren’t your style, PowerShell has you covered! Just as with the kill command, you can terminate processes using the Stop-Process cmdlet in Linux. Once again, pretend you are running a PowerShell pwsh process.
- Find the PID with Get-Process -ProcessName ‘pwsh’ and note the PIDs to terminate.
- Use Stop-Process -Id 29992,30014 to terminate the multiple processes returned.
- Use Get-Process -ProcessName ‘pwsh’ and verify the PIDs, 29992 and 30014 are no longer listed, meaning they have been terminated.
You can also use Get-Process and the kill method to terminate one or multiple processes: (Get-Process -Id [PID]).Kill() or (Get-Process -Id [PID1,2,3,4]).Kill() .
Exercise caution when using the ProcessName parameter. Using this parameter behaves similar to the killall command, and will kill all processes matching the name(s) provided.
Below is an example of the above process from an Ubuntu 20.04 LTS bash terminal.
Next Steps
You should now know the many ways to find and terminate processes in Linux! For your next challenge, try killing processes remotely using SSH connections or by killing processes that may be running in containers.
More from Adam The Automator & Friends
With an ever-increasing visitor base searching for solutions to their problems, we can help you connect with and reach the right kind of audience and generate targeted sales leads.
We’ve put together a list of the resources we, at ATA, can wholeheartedly recommend!
Why not write on a platform with an existing audience and share your knowledge with the world?
Источник
Use killall and kill Commands to Stop Processes on Linux
killall is a tool for terminating running processes on your system based on name. In contrast, kill terminates processes based on Process ID number (PID). kill and killall can also send specific system signals to processes.
Use killall and kill in conjunction with tools including Process Status, ps , to manage and end processes that have become stuck or unresponsive.
Throughout this guide, replace [process name] in each example with the name of the process you wish to terminate.
Usage
How to Use killall
The killall command takes the following form:
killall will terminate all programs that match the name specified. Without additional arguments, killall sends SIGTERM , or signal number 15, which terminates running processes that match the name specified. You may specify a different signal using the -s option as follows:
This sends the SIGKILL signal which is more successful at ending a particularly unruly processes. You may also specify signals in one of the following formats:
How to Use kill
The kill command terminates individual processes as specified by their PID.
Commands take the following form:
Without options, kill sends SIGTERM to the PID specified and asks the application or service to shut itself down. This is discussed further in the following section.
Multiple PIDs and alternate system signals can be specified within a single kill command. The following examples all send the SIGKILL signal to the PID specified:
System Signals
The kill command does not terminate a process directly. Rather, a signal is sent to the process where the process will have instructions to follow if it receives a given signal. The man pages provide further reference of all available signals:
To simply list all available signals without their descriptions:
If you need to convert a signal name into a signal number, or a signal number into a signal name, use the following as examples:
Find Running Processes
Use a utility like htop or top to view a real time list of process and their consumption of system resources.
Use the ps command to view processes that are currently running and their PIDs. The following example filters the list of all processes that are currently running for the string emacs using grep:
The number listed in the second column from the left is the PID, which is 3896 in the case of the emacs process. The grep process will always match itself for a simple search, as in the second result.
Once you have obtained the PID or process name, use killall or kill to terminate the process as above.
Another option to find the PID is though pgrep .
Verify Process Termination
Adding the -w option to a killall command causes killall to wait until the process terminates before exiting. Consider the following command:
This example issues the SIGTERM system signal to a background process with a name that matches irssi . killall will wait until the matched processes ends. If no process matches the name specified, killall returns an error message:
This page was originally published on Monday, November 29, 2010.
Источник