Managing processes in Linux can appear daunting to a new system administrator (sysadmin) at first. But with a picayune explanation and sit-in, y'all'll learn how to find and kill processes in Linux is merely a matter of using command-line tools such as ps and impale.
In this commodity, you volition larn 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, yous 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!
Prerequisites
This commodity is going to be a walk-through of how to manage processes in Linux. If you want to exist hands-on, you will need the following:
A Linux host, which can be a virtual automobile or a physical automobile.
To try the PowerShell examples, install PowerShell 7.1.1 on a Linux host.
A non-root user account. While root access is non required, there will be an instance where the root user can kill the running process of another user.
The distribution of Linux, yous cull to apply, is upward to y'all. This article will be based on Ubuntu xx.04 LTS. Yet, the commands are standard for the Linux operating organisation.
Ready to get started? Skillful, allow's dive in.
Managing Processes in Linux
On occasion, y'all may come across a scenario where y'all need to stop a running process on a Linux system. As an instance, you may have a bash script that runs on a schedule. That bash script is configured to run every bit another user via a cron chore (Linux procedure scheduler).
Sometimes, this script spawns errant processes that tin make the script neglect. How exercise you lot kill those errant processes without rebooting the system or stopping the script?
In the side by side sections, you will walk through commands used to locate and terminate or impale processes on a Linux system. Each department will build on the last. You will learn almost ps, top, pgrep, kill, and pkill commands all native to Linux.
Equally a bonus, PowerShell equivalent commands Get-Process and Cease-Procedure are included if Linux native commands are not your manner.
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 Condition or ps control displays information related to active processes on a Linux organization. You can locate a running procedure and even background processes with ps.
The control ps will, past default, display all of the processes for the effective user identification (EUID) of the current user. Running the ps command, every bit shown below, will render every running process that your user can end.
Finding Expanded Procedure Data 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 acme control. Unlike ps, the acme command updates the process information on the screen over a prepare interval.
When you run the top command, you will meet more than than just a PID. Included with process details are CPU and memory percentages, Squeamish values (CPU scheduling process priority), and the process priority (PR) prepare by the Linux kernel.
For a new or experienced sysadmin, the top control is often the primary tool for managing vital system resources and processes. Equally shown beneath, 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 audit the retentiveness or CPU resources a process is consuming. In that case, you tin can use the p switch of height as shown below.
Peradventure you demand to display all running processes for a specific username. The peak command offers the u switch to display only a specific user'south processes. As seen in the example below, PIDs matching merely 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 ability of the grep command and the process management of ps, pgrep offers flexibility in finding the verbal procedure 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 post-obit case, 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, simply a single PID is returned for the user bkindle and the vmtoolsd process.
pgrep -u bkindle vmtoolsd
Finding Processes with PowerShell's Get-Procedure in Linux
Typical Linux commands piece of work groovy, but did you lot know PowerShell is able to manage Linux processes as well? The Get-Process cmdlet works like to the Linux ps command and returns process data. For example, peradventure you need to detect all running processes past the current user, bkindle. As shown below, observe all PIDs (labeled ID by Get-Process) of the user bkindle.
Related: How to Use PowerShell Where-Object to Filter All the Things
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 procedure you lot desire to send a kill signal to using native Linux and PowerShell commands. The side by side sections will explicate 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 organization. The iii main signals that the kill command uses to end processes are:
1 (SIGHUP) – Terminates interactive programs and causes daemons (groundwork services) to re-read the configuration files the process uses.
ix (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 bespeak used when no number is specified when using the kill command.
While this article only focuses on the iii main signals used for killing processes, at that place are a total of 64 bachelor signals. Use kill -fifty to get the list of signals and their respective number.
At present that you understand the three main signals used in terminating a procedure, learn how to kill a process in the next sections.
Using the kill Command to Terminate a Procedure
In this next instance, you will employ the kill command. Pretend for a moment that you lot are running a PowerShell instance, named pwsh, and the PID assigned is 22687. One way to terminate this process is shown below.
Utilize pgrep pwsh to determine the PID for the process pwsh, used in the impale command.
Utilize kill -due south TERM 22687 to terminate the pwsh process gracefully. The TERM command maps to the 15 (SIGTERM) bespeak and indicated using the due south parameter of the kill command.
Use your choice of tiptop, ps, or pgrep to verify the PID is no longer listed.
Shown beneath is an example of the above process from an Ubuntu twenty.04 LTS fustigate terminal.
The killall command will terminate all processes based on a proper noun instead of PID, which can make this command pretty destructive if used carelessly. If you lot cull to employ this control, however, utilize the i option to crusade the control to enquire for confirmation.
Killing Processes by Proper name with pkill
You lot may have noticed that the impale control only works with process IDs. This can make terminating a process a complicated multi-step procedure. The pkillcommand, 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 just listing the PIDs in the final. 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 lot will have to download and install the application. To do so, run sudo apt install mcollective-plugins-process -y.
To kill a procedure 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, only a rubber check before killing a process.
Use pkill -9 nano to forcefully terminate the nano application.
Shown below is an example of the higher up process from an Ubuntu 20.04 LTS bash final.
Killing a Procedure with the Top Command
When you run the top command to view running processes, you can kill a given process with the thou keystroke. You'll meet an interactive prompt request for the PID of a process to kill.
As shown below, run the top command and press k. Enter the PID that yous desire to impale, and printing the Enter key to immediately terminate the PID.
Side by side, larn one more than culling to native Linux commands using the PowerShell Stop-Process command!
Using the PowerShell Stop-Process Command to Cease a Process
If Linux native commands aren't your style, PowerShell has y'all covered! Just equally with the kill control, yous can cease processes using theFinish-Process cmdlet in Linux. Once once again, pretend you are running a PowerShell pwsh process.
Detect the PID with Get-Process -ProcessName 'pwsh' and note the PIDs to terminate.
Employ Stop-Process -Id 29992,30014 to terminate the multiple processes returned.
Use Get-Procedure -ProcessName 'pwsh' and verify the PIDs, 29992 and 30014 are no longer listed, meaning they take been terminated.
You tin can also use Become-Procedure and the kill method to end i or multiple processes: (Go-Procedure -Id [PID]).Impale() or (Get-Process -Id [PID1,2,3,4]).Kill().
Practice caution when using the ProcessName parameter. Using this parameter behaves similar to the killall command, and will kill all processes matching the proper name(southward) provided.
Below is an example of the above process from an Ubuntu xx.04 LTS bash terminal.
Next Steps
You should now know the many ways to discover and terminate processes in Linux! For your next challenge, endeavor killing processes remotely using SSH connections or by killing processes that may be running in containers.
Related: A Windows Guy in a Linux World: Setting upwards SSH in Linux
0 Response to "After Kill Process on Linux It Runs Again on Another Pid"
Postar um comentário