How to Locate and Kill Processes in Linux

How to Locate and Kill Processes in Linux

Running any system comes with all sorts of unintended consensuses like encoutering unresponsive apps, frozen process and much more. Today we are going to take a look at how we can mitigate that when running Linux. The operating system comes with a hand full of command-line tools that makes this real simple.

On top of that, most Linux desktop environment provide GUI applications that can be used if you are not familiar with the command-line. However, this tutorial will focus solely on how to kill running and unresponsive Linux process using the terminal.

1. View and Locate Running Processes

First of all you need to understand what process is causing issues and also what PID it uses.

A great command-line tool for viewing process on Linux is top. The command allows you to monitor processes and system resource usage on your Linux machine. It is one of the most useful tools in the sysadmin’s toolbox, and it comes pre-installed on every Linux distribution. It's perfect for server administrators that want to monitor RAM and CPU usage, inspect tasks, and view system uptime and user sessions, all in real-time.

Run Top:

top

Output:

top - 12:05:23 up  8:37,  1 user,  load average: 0.38, 0.61, 0.89
Tasks: 477 total,   1 running, 476 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.6 us,  0.6 sy,  0.0 ni, 98.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :  15424.1 total,   4643.7 free,   5416.7 used,   5363.7 buff/cache
MiB Swap:   8192.0 total,   8180.5 free,     11.5 used.   9672.8 avail Mem 

PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                                                               
2504 username    20   0 7368784 338016 145100 S   8.3   2.1  62:40.57 gnome-shell                                                                                           
19759 username    20   0  837020 105020  14500 S   4.6   0.7  19:10.31 python                                                                                                
17455 username    20   0  853404  65156  47340 S   3.0   0.4   0:57.94 gnome-terminal
  ...

Unlike other commands such as ps, top is fully interactive, and updates process in real-time. Which makes it a favorite of many Linux users and Sysadmins.

You can locate an unresponsive utilizing command line tools like top and ps. Top

2. Kill Running or Unresponsive Process

As we can see from the output of the top command, each process has a PID and COMMAND (name). To quit or kill a running or unresponsive process we will make use of them.

Kill a Process using PID

Once you’ve determined the PID of the process you wish to end, you can simply specify it as an argument with the kill command. As per the example above, the process "python" has a PID of 19759. To kill this running process we can utilize the kill command.

Run Kill:

kill 19759

The "python" process with a pid of "19759" is no longer running.

You can also use the ps command.

ps -e | grep 19759

Kill a Process using its Name

You can also kill a process by using its name with pkill command. In many cases this might be faster and easier to utilize.

pkill python

The process with the name "python" is no longer running. You can verify this by running top once again or simply verify from the list if you got it up and running.

Freddie Freddie 1 year, 7 months ago 0
Login to Comment
No comments have been posted yet, be the first one to comment.
How to install NodeJS on Fedora 35
How to install NodeJS on Fedora 35
The easiest way to install NodeJS on Fedora 35 is to use the dnf package manager. Use DNF to download NodeJS Simply Open up your terminal and run the following command. sudo dnf install nodejs Verify the packages to download After you have run sudo dnf install nodejs you will have to ...
How to Create SSH Keys for Linux
How to Create SSH Keys for Linux
An SSH key is an access credential utilized for the SSH protocol. Its function is similar to that of user names and passwords, but the keys are primarily used for accessing servers via the command line. Create SSH Key To create and SSH key on Linux we can use the command ssh-keygen. s...
How to Install Go on Linux
How to Install Go on Linux
Download the latest Linux version of Go You can download the latest binary from https://go.dev/dl/ Extract the archive you downloaded into /usr/local, creating a Go tree in /usr/local/go. Important: This step will remove a previous installation at /usr/local/go, if any, prior to extr...