> ## Content Index
> Fetch the complete content index at: https://linuxhandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Find the PID and PPID of a Process in Linux
- URL: https://linuxhandbook.com/find-process-id/
- Published: 2022-01-29T07:20:14.000Z
- Updated: 2023-09-16T09:20:42.000Z
- Description: Learn how to find PID using a process name in Linux. Also learn to get the parent process ID (PPID) of the given process.
- Author: Hunter Wittenborn
- Tags: Process Handling, Tips, #cli, #docs-col

Knowing the PID and PPID of a process can be helpful if you need to manage or interact with a process running on your system. 

There are numerous ways to get the PID (Process ID) and PPID (Parent Process ID) of a given process in Linux.

| Command             | Description                     |
| ------------------- | ------------------------------- |
| pidof process\_name | Works with exact process name   |
| pgrep process\_name | Returns PID of all matches      |
| ps -o ppid= -p PID  | Get the PPID from PID           |
| $$                  | PID of current process/shell    |
| ${PPID}             | PID of current process's parent |

I'll explain these commands in detail but before that a quick recap of process, PID and PPID.

## Linux process basics

Everything that runs on your system is ran via something known as a process, with that simply being the running instance of a program.

All the processes that run on your system are assigned identifiers. These can be helpful if you want to monitor the process (for example, such as to see how much memory or CPU it is using), or maybe if you want to end it if it starts to hang or just act a bit funky.

The identifiers that get attached to all these processes are known as PIDs and PPIDs.

### What is a PID?

PID stands for "process ID". Again, this is simply the identifier that gets attached to a program when it starts running, and can be helpful if you need to interact with the process in one way or another.

### What is a PPID?

PPID is quite closely related to a PID. PPID stands for "parent process ID", and if you didn't get it already, it simply stands for the process that *created* the process you are checking.

For example, let's say that we have two processes. One is named "spawner", and has a process ID (or PID) of 7234\. Our second process, "email client", has a process ID of 7456 when we create it. Our spawner program starts our email client, resulting in our email client having a PID of 7456, and a PPID of 7234, since the spawner (which had the PID of 7234) is what spawned the email client.

Now that you have brushed up your basic, let's see how to get the process ID in Linux.

## Getting the PID of a process

The important thing here is to [know the name of the process](https://linuxhandbook.com/process-name-from-pid/) whose PID you want to find.

**If you know the exact process name**, you can get its process ID using the `pidof` command:

```
pidof exact_process_name
```

Easier said than done because you may not always know the exact process name. Good thing here is that `pidof` command works with [tab completion](https://linuxhandbook.com/enable-tab-completion/) so if you know the starting few letters of the process name, you can hit tab to get matching suggestions.

![Example of pidof command which is used for finding PID of a process in Linux](https://linuxhandbook.com/content/images/2022/01/pidof-command-example-linux.png)

pidof command works with tab completion

However, that may not always work if the process name doesn't match to what you think it is called. For example, the process for Edge browser on Linux is called `msedge`. It doesn't start with 'edge' and the tab completion won't work if you focus on 'edge'.

So, what you can do is to resort to the [ps command in Linux](https://linuxhandbook.com/ps-command/) to list all the running processes from all users and then [use grep](https://linuxhandbook.com/grep-command-examples/) on the output to filter the result.

```
ps aux | grep -i partial_process_name
```

There is a dedicated command that combines the features ps and grep command and it is unsurprisingly called [pgrep](https://linuxhandbook.com/pgrep/):

```
pgrep partial_or_exact_process_name
```

![pgrep command](https://linuxhandbook.com/content/images/2022/01/image-5.png)

The default output shows only the PIDs without any information on the process. This could be troublesome if there is more than one process IDs returned for your searched term.

Hence, I suggest using the listing feature to make sure that you are getting the PID of the desired process.

```
pgrep -l partial_or_exact_process_name
```

![pgrep command example](https://linuxhandbook.com/content/images/2022/01/pgrep-linux-command-example.png)

You may also [use the top command](https://linuxhandbook.com/top-command/) to get the process information but it cannot be used in scripts.

💡

You can use the pstree command to get the PIDs of all running process on your Linux system: `pstree -p -a`

## Getting PPID from a child process's PID

Once you know the PID of a process, it is effortless to find the PPID for that process.

You can simply run the following command, replacing `PID` with the current process (child) ID:

```bash
ps -o ppid= -p PID
```

In a shell, the above command and `${PPID}` should both return the same output:

![](https://linuxhandbook.com/content/images/2022/01/image-6.png)

And that's about everything there is to finding PIDs and PPIDs!

## Checking the PID and PPID of the currently running process

If you're in a shell such as Bash, it's extremely easy to find the PID and PPID of the calling process (which will usually be the shell).

Bash stores the PID's value under the `$$` variable, and the PPID under the `${PPID}` variable:

```bash
# Prints the PID.
echo "$$"

# Prints the PPID.
echo "${PPID}"
```

Commands to find the PID and PPID of the current process.

This way, you can get the process ID of shell and [subshell](https://linuxhandbook.com/subshell/).

![PID and PPID of current shell](https://linuxhandbook.com/content/images/2022/01/image.png)

And it's that easy! Finding the PIDs and PPIDs of other processes isn't much harder either.

## Wrapping up

You should now know everything you need to find both PIDs and PPIDs for running processes on your system.

If you need any help getting something working, or just got some remaining questions, feel free to leave that and anything else in the comments below.