Mastering Linux Process Identification: Uncover PIDs Effortlessly with pidof and pgrep

Mastering Linux Process Identification: Uncover PIDs Effortlessly with pidof and pgrep

Discovering the Process ID (PID) of a Linux process is crucial for efficient system management Uncover the PID of a process effortlessly using the pidof or pgrep commands Gain insights into the concept of PID ownership

Key Takeaways

In order to determine the process ID of a Linux process, you can utilize the pidof command in the following manner: "pidof examplename". Alternatively, if you possess knowledge of only a portion of the PID name, you can employ "pgrep examplenamefragment" instead. It is important to substitute "examplename" and "examplenamefragment" with the specific terms you intend 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. Here are two ways to find out what it is.

What Is a Linux Process ID?

Linux internally manages its running processes by assigning them a distinct identification number known as the process ID (PID). Each active application, utility, and daemon is associated with a PID.

PIDs are represented as integer values, and a newly initiated process is assigned a PID that is one greater than the previously issued PID. Therefore, the process with the highest PID corresponds to the most recently launched process. This sequence continues until the system reaches its maximum PID value.

The maximum limit for a PID is 32768. When this limit is reached, Linux will restart the process and search for a PID that has become available due to the termination of its previous owner.

The process with a PID of 1 is the initial process launched by the boot-up processes when Linux is started. On systems that use systemd, this process is typically systemd. On other systems, it is likely to be init, although certain Linux distributions may utilize alternatives such as OpenRc or s6.

Sometimes, it can be beneficial to identify the PID (Process IDentifier) of a particular process, typically when you intend to execute certain actions related to that process. There are two distinct approaches to determine the PID of a process when you are aware of its name.

How to Get a Linux PID With the pidof Command

When using the pidof command, it can be seen as a combination of the "PID" and "of." It functions by inquiring about the PID of a specific process. If the command is used without any parameters, no action will be taken, and it will simply return you to the command prompt. However, it is necessary to indicate the process name that you are interested in.

Mastering Linux Process Identification: Uncover PIDs Effortlessly with pidof and pgrep

The PID of the Bash shell is 8304 as revealed by the pidof command. This can be further confirmed by using the ps command. Simply running ps without any arguments will provide a report on the processes currently active in the ongoing session.

ps

Mastering Linux Process Identification: Uncover PIDs Effortlessly with pidof and pgrep

The content fragment is:

"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."

The rewritten version is:

"Ps provides a report on all processes it can identify, including itself. This report confirms the presence of a bash process and a ps process. As anticipated, the PID reported by pidof matches that of the bash process.

When multiple terminal windows are active, pidof will generate a report encompassing all of them."

pidof bash

Mastering Linux Process Identification: Uncover PIDs Effortlessly with pidof and pgrep

The PIDs are listed in descending order, from latest to oldest, indicating the succession of processes.

However, it is important to note that not all of these processes may be owned by you. pidof retrieves processes with matching names, regardless of the owner. To gain a better understanding, we can further analyze the output by using grep and piping it with the ps command. Specifically, we are utilizing the -e (select all processes) and -f (full listing) options with ps.

ps -ef | grep bash

Mastering Linux Process Identification: Uncover PIDs Effortlessly with pidof and pgrep

Two bash processes are assigned to user dave, while the remaining one belongs to user mary. In the case of Google Chrome, it frequently generates multiple processes, with each process having its own unique PID.

pidof chrome

Mastering Linux Process Identification: Uncover PIDs Effortlessly with pidof and pgrep

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

Mastering Linux Process Identification: Uncover PIDs Effortlessly with pidof and pgrep

Manually killing all of the chrome processes using the kill command can be a tedious task. However, we can simplify this process by capturing the list of processes into a variable and passing it to the kill command. Since the kill command can accept multiple PIDs, we can effortlessly input our variable and terminate all of the processes simultaneously.

pid=$(pidof chrome)

echo $pid

kill $pid

pidof chrome

Mastering Linux Process Identification: Uncover PIDs Effortlessly with pidof and pgrep

The initial instruction,

Mastering Linux Process Identification: Uncover PIDs Effortlessly with pidof and pgrep

, carries out the task of collecting the output from pidof and subsequently assigning it to the designated variable, referred to as pid. The act of displaying the variable's content on the screen through echoing is not necessarily required, but is merely carried out in this instance for demonstrative purposes.

We pass the variable to the kill command and then recheck if any Chrome processes are left using pidof once more. All of them have been successfully terminated.

An interesting aspect of pidof is its behavior to not return the PID of a shell script. Instead, it returns the PID of the bash shell executing the script. To identify the shell running a script, the -x (scripts) option is employed.

pidof -x sleep-loop.sh

ps -e | grep bash

Mastering Linux Process Identification: Uncover PIDs Effortlessly with pidof and pgrep

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 functions similar to pidof in Linux as it retrieves process IDs. However, it not only identifies processes that directly match the search criteria, but also provides the PIDs of any processes whose names incorporate the search term.

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

pgrep firefox

pgrep fire

pgrep fox

pgrep refo

Mastering Linux Process Identification: Uncover PIDs Effortlessly with pidof and pgrep

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

Mastering Linux Process Identification: Uncover PIDs Effortlessly with pidof and pgrep

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

pgrep bash

Mastering Linux Process Identification: Uncover PIDs Effortlessly with pidof and pgrep

The listed processes are in ascending order, contrary to the output from pidof. They are arranged from the oldest process to the newest process. However, it should be noted that like the processes identified by pidof, not all of the processes listed here may be attributed 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

Mastering Linux Process Identification: Uncover PIDs Effortlessly with pidof and pgrep

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

pgrep bash -u mary

Mastering Linux Process Identification: Uncover PIDs Effortlessly with pidof and pgrep

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

pgrep bash -u dave,mary -l

Mastering Linux Process Identification: Uncover PIDs Effortlessly with pidof and pgrep

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

pgrep -u dave -l

Mastering Linux Process Identification: Uncover PIDs Effortlessly with pidof and pgrep

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

pgrep -u dave -a

Mastering Linux Process Identification: Uncover PIDs Effortlessly with pidof and pgrep

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.

Linux Commands

Files

tar · pv · cat · tac · chmod · grep · diff · sed · ar · man · pushd · popd · fsck · testdisk · seq · fd · pandoc · cd · $PATH · awk · join · jq · fold · uniq · journalctl · tail · stat · ls · fstab · echo · less · chgrp · chown · rev · look · strings · type · rename · zip · unzip · mount · umount · install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · vi · nano · mkdir · du · ln · patch · convert · rclone · shred · srm · scp · gzip · chattr · cut · find · umask · wc · tr

Processes

alias · screen · top · nice · renice · progress · strace · systemd · tmux · chsh · history · at · batch · free · which · dmesg · chfn · usermod · ps · chroot · xargs · tty · pinky · lsof · vmstat · timeout · wall · yes · kill · sleep · sudo · su · time · groupadd · usermod · groups · lshw · shutdown · reboot · halt · poweroff · passwd · lscpu · crontab · date · bg · fg · pidof · nohup · pmap

Networking

netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw · arping · firewalld