How to Find the PID of a Linux Process With pidof or pgrep

Configurare noua (How To)

Situatie

To find the process ID of a Linux process, use the pidof command, like this: “pidof examplename”. If you only know part of the PID name, you can use “pgrep examplenamefragment” instead. Replace “examplename” and “examplenamefragment” with the terms you want to search for.

Working with a Linux process often means knowing its process ID, or PID. It’s a unique number given to each piece of running software.

Solutie

What Is a Linux Process ID?

Internally, Linux keeps track of its running process by allocating them a unique ID number, called the process ID, or PID. Every running application, utility, and daemon has a PID. PIDs are simple integer values. A newly-started process will receive a PID one higher than the last PID that was issued. So the process with the highest PID is the newest—that is, most recently—launched process. That carries on until the system hits the maximum value for a PID.

The upper limit for a PID is 32768. Once that figure is reached, Linux goes back to the start and looks for a PID that has become free because the process that previously owned it has terminated.

The process with a PID of 1 is the first process that is launched when Linux is launched by the boot-up processes. On systemd-based systems that’ll be systemd . On other systems it is likely to be init, although some Linux distributions use alternatives such as OpenRc or s6.

Sometimes it’s useful to discover the PID of a process, usually because you want to perform some action on that process. Here are two different methods of finding the PID of a process when you know the name of the process.

How to Get a Linux PID With the pidof Command

The pidof command can be thought of as the combination of “PID” and “of.” It’s like asking what’s the PID of this process? If we use the command with no parameters it doesn’t do anything. It silently returns you to the command prompt. We need to specify a process name.

pidof bash

Finding the PID of a bash shell with the pidof command

pidof tells us the PID of the Bash shell is 8304. We can verify that with the ps command. All we need to do is call ps with no parameters. It’ll report on the processes that are running in the current session.

ps

listing processes with the ps command

Because ps reports on all the processes it can find, which will include itself, it tells us there’s a bash process and a ps process running. As we’d expect, the bash process has the same PID that pidof reported on.

If you have more than one terminal window open, pidof will report on them all.

pidof bash

pidof reporting on several instances of matching processes

Note that the PIDs are listed from highest to lowest or, in other words, from most recent to the oldest.

What this doesn’t show is that you might not be the owner of all of those processes. pidof finds all processes with matching names, regardless of who owns them. Let’s look deeper by piping the output into grep. We’re using the -e (select all processes) and the -f (full listing) options with ps .

ps -ef | grep bash

Using ps and grep to identify the owners of bash processes

Two of the bash processes belong to user dave, the third belongs to user mary.

Sometimes one application will generate a lot of processes, each of which receives its own PID. This is what we get with Google Chrome.

pidof chrome

pidof finding many PIDs for a single application with many processes

By default, pidof reports on all processes. If we want, we can ask for just the most recent of those processes. The -s (single shot) option does just that.

pidof -s chrome

Finding the most recent PID from n application with multiple PIDs

To use the kill command to manually kill all of the chrome processes would be tedious. If we capture the list of processes into a variable, we can pass that variable to the kill command. The kill command can accept multiple PIDs on its command, so it happily accepts our input and kills all of the processes for us.

pid=$(pidof chrome)
echo $pid
kill $pid
pidof chrome

Passing a variable containing many PIDs to the kill command

The first command collects the output from pidof and assigns it to our variable, which we’re naming pid. We don’t need to echo it to the screen, we’re just doing that to show what our variable holds.

We pass the variable to the kill command, then use pidof once more to check whether any Chrome processes remain. They have all been killed.

One quirk of pidof is that it won’t return the PID of a shell script. It returns the PID of the bash shell that is running the script. To see the shell that is running a script, we need to use the -x (scripts) option.

pidof -x sleep-loop.sh
ps -e | grep bash

Finding the PID of a bash shell running a shell script

pidof returns the PID of a bash shell, and ps shows us there are two shells running. One is the shell running the pidof command, and the other is the shell running the script.

How to Find PIDs With the pgrep Command in Linux

The pgrep command works a little like pidof in getting process IDs in Linux. However, it doesn’t just find processes that exactly match the search clue, it also returns the PIDs of any processes whose name contains the search text.

Here’s an example on a computer that has Firefox running on it.

pgrep firefox
pgrep fire
pgrep fox
pgrep refo

Finding the PID of Firefox using different search clues

All of these commands find the Firefox process and return the PID. But if you’d entered the command:

pgrep refo

On its own, how would you know if pgrep had found Firefox and not, say, a dameon called preformd?

If you add the -l (list name) option, pgrep will list the process name alongside the PID.

pgrep refo -l

Using the -l option to make pgrep list the process name

If there are multiple instances of a matching process, they are all listed.

pgrep bash

Listing multiple PIDs with pgrep

Note that they are listed in ascending order, which is the opposite order to the output from pidof. They are listed from the oldest process to newest process. As we saw with pidof, not all of the listed processes necessarily belong to you.

The -u (user id) option lets you search for processes that match the search text, and are owned by the named user.

pgrep bash -u dave

Listing user dave's bash processes PIDs

This time we see three bash processes in the results. The other is being used by mary.

pgrep bash -u mary

Listing user mary's bash processes PIDs

We can string usernames together as a comma-separated list.

pgrep bash -u dave,mary -l

Listing two user's bash processes PIDs

And we can ask to see all processes for a specific user.

pgrep -u dave -l

Listing all PIDs owned by a specific user

To see the full command line, use the -a (full list) option.

pgrep -u dave -a

Using the -a option to list the entire command line of each process

A Word About PID Ownership

Not all system processes are owned by the root user. Many are, of course, but not all of them. For example, this command works:

pgrep avahi-daemon

But this command fails.

pgrep -u root avahi-daemon

It fails because root does not own that process. The actual owner is a system user called “avahi.” Using the correct user name, the command works.

pgrep -u avahi avahi-daemon

It’s a little gotcha to watch out for.

Tip solutie

Permanent

Voteaza

(10 din 20 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?