Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Generic User Avatar
- - - - -

Find the PID (Process ID Number) and Kill the Process, using the Terminal


  • Please log in to reply
3 replies to this topic

#1 Naught McNoone

Naught McNoone

  •  Avatar image
  • Members
  • 806 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:The Great White North
  • Local time:01:39 AM

Posted 07 June 2021 - 03:47 PM

Find the PID (Process ID Number) and Kill the Process, using the Terminal

 

 

Linux users do not like to admit it, but, there are occasions when you get stuck in a window, or a desktop and there seems to be no way out, short of hitting the reset button.

The problem with that is that all your unsaved work goes away, along with the offending program.

 

 

 

Guide Overview

The purpose of this tutorial is to show you how to recover from a frozen desktop, without having to reboot.

I will be showing you four different options, each with it's own features, to find the PID of a running process using the terminal command line interface.

CAUTION:  Randomly killing processes can cause your system to become unstable.  Follow the instructions.  Do NOT guess at your PID numbers.


Tools Needed:
A working Linux system.  This tutorial should not be specific to any one distribution.
 

 

Preparation:
When a Desktop has become unresponsive, our first choice is to open the "Task Manager" in the menu, under "System".  However, if your screen is completely frozen, then you will not be able to do that.

That does not necessarily mean that your computer is completely locked up.  Most users who are familiar with "Terminal" emulators know the keyboard combination to open a Terminal window.
 
For the novice, all you have to do is press the following three keys simultaneously:

 

<ctl>+<alt>+<t>

 

This will open a terminal window open on your desktop.  However, that terminal window is still presented as part of the GUI desktop.  As such, it too can be unresponsive.

Things are not always lost, just because we can't see them.

You can open a true terminal, outside the X11 windows system, and enter your commands through it.

The keystroke combination to switch from GUI to terminal is:


<ctl>+<alt>+<f1>

 

Pressing these three keys together will put your GUI desktop into the bacground, and bring up the TTY (TeleType) interface.  So called because back in the day, I acutally accessed the server using an IBM Selectric TeleType Writer.

You simply log in with your user name and password, and you will have full privileges of the system, but without the GUI.

To return to your Desktop, use this combination:


<ctl>+<alt>+<f7>

 

You don't have to log our of the terminal.  You can switch back and forth, between the two.

Try it!


Step :step1:    How to find the PID of a running process by name.

 

Once you have an active terminal, you can use several commands to solve your issue.

Lets' look at the simplest one first.  Do you know the name of the process that you want to terminate?  If so, then a simple command will give us it's PID.

For our purpose, I am going to use the Firefox browser.

The command is:


pgrep firefox

 

This command will return a numeric string.  That number is the PID, or Process ID Number of Firefox.

Using the "kill" command to terminate a process.
To terminate the program, enter the "kill" command, using the PID:


sudo kill -9 nnnnnn

 

NOTE:  You have to be the "Super User" or system administrator in order to kill a process.
You have to provide your user name and password, before it will do so.

Why -9?
The switch, -9 tells the kill command to terminate.  It tells the kill command to send the "SIGKILL" signal to the process.  This is the fastest way to kill a GUI window.

 

Substitute the actual PID number of the process you want to kill for "nnnnnn"

If we go back to our desktop, <ctl>+<alt>+<f7>, the Firefox window will be gone, not just suspended.
 

 

Step :step2:  If you don't know the process name.

 

If you don't know the process name you can manually search for it.

What name the process has for a title in the menu, may not be the exact name the the system knows it by.

For example, the program gnome-disks appears in the menu under the name "Disks".  When I use pgrep disks, it comes back with more than one PID, of which only one is actually the program I want to kill.  So how do I know which is which?

To list all running processes.
The command to list all running processes is:


ps -aux

 

The -a and x switches lift restrictions, to show all running processes.
The -u switch says to list the users.

Another way to enter the command is to use the "| less" pipe.


ps -aux | less

 
You will get a print out to the screen, listed page by page.

Pressing "enter" will scroll through, one line at a time.

Pressing "pgdn" will scroll through, one page at a time.
 

Simply scroll down until you find the program you want to kill, and note its' PID.

 

The advantage to this is that you get a frozen snapshot of all running processes.

In the GUI Task Manager, the list constantly changes as programs do things in the background.

 

 

Step  :step3:  Is there something more "Human Readable" that I can use?

 

Fortuanately we have a more human readable way to find PID's.

Almost all newer distributions of Linux now come with a program called "top" installed as default.

Simply enter the command:


top

 

and you will see a text based version of your GUI task manager.

 

It behaves the same as the GUI Task Manager, in that as memory usage changes, so does the listing.

To exit from top, just type "q".


The best for the last.

I recommend that if you are more command line oriented, that you should install a program called "htop".

htop is a full text based program, that offers f-key functions, search, filter, kill, and others.

To install the repository version of htop simply use:


sudo apt update
sudo apt install htop

 

It looks best in a full window, or the TTY screen.

 

Cheers!

Naught     :busy:




 


Edited by Mike_Walsh, 07 June 2021 - 07:37 PM.


BC AdBot (Login to Remove)

 


#2 Naught McNoone

Naught McNoone
  • Topic Starter

  •  Avatar image
  • Members
  • 806 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:The Great White North
  • Local time:01:39 AM

Posted 07 June 2021 - 07:00 PM

:oopsign:

 

OK, so, a couple of typo's.

 

:mellow:

I meant to say "We  actually accessed the server using an IBM Selectric TeleType Writer."  (And yes, I did use one, too!)

 

There is no Step :step2:, but there are 2 Step :step3: 's?

 

My apologies.

 

I'm getting too old for this.

 

Naught :(

 

 



#3 cryptodan

cryptodan

    Bleepin Madman


  •  Avatar image
  • Members
  • 34,434 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:USA
  • Local time:05:39 AM

Posted 07 June 2021 - 07:36 PM

The following is what I use to find a process.

ps auxww | grep -i nnnnnnn

Where nnnnnn is the name of the process

The ww tells ps to word wrap the process. It us a more granular method to pinpoint the exact process. It gives more detail about the running process.

US Navy Veteran from 2002 to 2006

Masters in Computer and Digital Forensics Expert - Stevenson University Alumni 2015

Arch Desktop - https://termbin.com/epij

Arch Laptop - https://www.termbin.com/dnwk

Ubuntu Server - https://termbin.com/zvra


#4 Mike_Walsh

Mike_Walsh

    Bleepin' "Puppy" fanatic...


  •  Avatar image
  • Moderator
  • 4,760 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:King's Lynn, UK
  • Local time:06:39 AM

Posted 07 June 2021 - 07:39 PM

@Naught:-

 

I've put your 'Step 2' in the correct place, mate. (Not to worry; we ALL get days like this, y'know..!)

 

:lol:  :lol:

 

 

Mike.  :wink:


Edited by Mike_Walsh, 08 June 2021 - 04:57 AM.

Distros:- Nowt but Puppies.....
My Puppy Packages ~~~ MORE Packages ~~~ ....and STILL more!
HP Pavilion mid-size tower - 590-p0024na; Pentium 'Gold' G5400 dual-core with H/T @ 3.7 GHz; 32 GB DDR4 RAM; Nvidia GeForce GT710 graphics (2 GB GDDR5) with 'passive' cooler; 1 TB Crucial MX500 SSD primary;  3 TB Seagate Barracuda HDD secondary; 1920x1080 HP 22w LED monitor; 7-port powered USB 2.0 hub; Logitech c920 HD 'Pro' webcam

 

forum-siggy-small.png
 
 





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users