How to Find PID and Kill a Process in Linux: A Step by Step Guide

Sohail
Sohail

Table of Contents

There comes a time where you need to find information about a certain process, often to find out what its ID is to parse to the ‘kill’ command. However, it need not necessarily be for that, of course. In this article we will learn how to find a process id of program and kill a process in Linux.

How to find a process id using ‘ps’?

Short for process state, ‘ps‘ is a tool that can read all of the process information on your computer, display the output on the terminal and exit. Information can include the process ID, user, and group it belongs to, the running state, and whether it was launched from a terminal.

It can also be used to list certain processes or all of them. It accepts standard style arguments (with a dash e.g. ‘-e’) as well as BSD-style arguments (without a dash), and GNU-style syntax (those preceded by two dashes). Consult the man page for more information.

How to use ‘ps’ to find process id of a program?

​Like many Linux commands, this one is simple to use. When executed with no arguments, it will print out some process information about ‘ps’ and the shell that ‘ps’ ran from. Here is an overview of some of the ways you can use ps.

Top 10 Linux Network Monitoring Tools

Keeping control of our network is vital to prevent any program from overusing it and slows down the overall system operation. There are several network monitoring tools for different operating systems today.

Listing All Processes

You can use ps to show all of the processes running on your computer. To print a list of all processes and their details, use the ‘-e’ parameter to list all processes.

$ ps -e

For extra information about a process including its parent process ID (PID), and the username it belongs to, couple it with the –‘f‘ parameter.

$ ps -ef
ps -ef

​For even more information, use the ‘-F’ parameter.$ ps -eF

ps process monitor on linux

Filtering Output

​If you only wish to view information about a specific process, or processes belonging to certain users, or filter by process names, there certainly some options to take advantage of. For instance, if you wanted to list all processes belonging to a user, such as a root, you would run this command:

$ ps -u root -U root u
ps user to find process id by user

As you can see the above output contains PID (process id). This is the process ID we can use to act upon this specific process.

Specific Processes

Once you have the process of a program, you can also view detailed information about it using the ‘-p’ parameter.

$ ps -lp <process-id>
ps -lp

The ‘-l’ parameter is for displaying extra information, like the ‘-f’ parameter with a few differences, like the username isn’t displayed, but the user ID (UID) is.

Show Threads

Threads can also be shown alongside processes using the ‘L’ parameter, which will display the LWP column. This is where you will see IDs for each thread associated with a process. The ID of the first thread will always be identical to the process ID it came from.

$ ps -eLf
ps -eLf
ps -eLf linux tool

Use the ‘-p PID’ argument to show threads from a particular process: 

$ ps -Lfp <PID>
ps -Lfp

Find process ID by program name

You may pipe output from ps to grep if you are looking for specific information that you may not be easily able to obtain using the parameters. This method can be used to find certain processes with a specific name.

$ ps -ef | grep chromium-browser
ps and grep to find process id by program name

Choosing What To Show

Using the ‘-o’ parameter allows you to define your own output format. You can choose to show the PID, PPID, user, and the command and its arguments in the output, for example.

$ ps -eo pid,ppid,%cpu,%mem,uname,comm,args
ps -eo

Find a process id using ‘top’

Like ps, the top is a program that lists information about processes in the system. Instead of printing a list to the terminal, though, it provides a real-time view of the processes with memory and CPU usages shown.

When you run top without any arguments, it shows a full list of processes with the process that has the highest CPU usage at the top.

Navigation is best done using the PgUp and PgDn buttons, with the Home and End buttons used to go to the top and bottom of the list, respectively. As with many things, the man page contains a lot of excellent information about its usage.​

Here is top launched with no arguments:

top no args

Show All Processes Belonging To A User

​Top will accept either the username or UID using the ‘-u’ parameter to only show the processes belonging to that user.

$ top -u <UID|Username>
view process id of programs

Monitoring a Specific Process

You can use the ‘-p’ argument to parse a process ID to top and monitor its activity.

$ top -p <PID>
top single process

Choosing What To Show

Like ps, you can have top display as much or as little information about each process by specifying columns to show. You can change which columns to show by pressing ‘f’ while top is running.

Use the up and down arrow buttons to navigate, and press the space bar to select and deselect the columns. You can even change the sorting of the process list by pressing ‘s’ on the selected column.

top sorting and columns by process id

Showing Threads

​Like ps, top can show individual threads from each process together with the processes using the ‘-H’ parameter. The thread ID will be listed under the PID column.

$ top -H

To show threads from a particular process, run this:

$ top -Hp <PID>
top threads kill a process in linux

Graphical Tools

Each desktop environment typically comes with their own apps for viewing process activity. They typically feature graphs representing memory and CPU usage as well as network and disk usage by a process.

How to kill a process in Linux?

Lastly we can use the above obtained information to kill a process. We can use the process id to send a TERM signal to a process. The kill command will kill the process.

$ kill <PID>

If this doesn’t do the trick, you can have it send the KILL signal (‘-9’ parameter), which cannot be caught by programs. This is the last resort, though.

Conclusion

Displaying process information is a simple process in Linux using either ‘ps‘ or ‘top‘ from the terminal. You can control how much detail you wish to view the processes that are running or sleeping on your system.

Once you have known the process ID of a program, you can use the process id to kill a process in Linux. For more in-depth information, you can read man pages.

Frequently Asked Questions

What does kill the process mean?

In computing, killing a process refers to terminating or stopping a running program or task on an operating system. When you kill a process, you are stopping it from running, which can be helpful in situations where a program becomes unresponsive, consumes too many system resources, or is no longer needed.
Linux toolsUncategorizedhow-to