Summary

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. Here are two ways to find out what it is.

listing processes with the ps command

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.

Using ps and grep to identify the owners of bash processes

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 besystemd. On other systems it is likely to beinit, although some Linux distributions use alternatives such asOpenRcors6.

Passing a variable containing many PIDs to the kill command

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

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

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

Finding the PID of a bash shell running a shell script

Becausepsreports on all the processes it can find, which will include itself, it tells us there’s abashprocess and apsprocess running. As we’d expect, thebashprocess has the same PID thatpidofreported on.

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

Finding the PID of Firefox using different search clues

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.pidoffinds all processes with matching names, regardless of who owns them. Let’s look deeper by piping the output intogrep. We’re using the-e(select all processes) and the-f(full listing) options withps.

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.

By default,pidofreports 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.

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

The first command collects the output frompidofand assigns it to our variable, which we’re namingpid. We don’t need toechoit to the screen, we’re just doing that to show what our variable holds.

We pass the variable to thekillcommand, then usepidofonce more to check whether any Chrome processes remain. They have all been killed.

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

pidofreturns the PID of a bash shell, andpsshows us there are two shells running. One is the shell running thepidofcommand, and the other is the shell running the script.

How to Find PIDs With the pgrep Command in Linux

Thepgrepcommand works a little likepidofin 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.

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

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.

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

Note that they are listed in ascending order, which is the opposite order to the output frompidof. They are listed from the oldest process to newest process. As we saw withpidof, 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 nameduser.

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

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

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

A Word About PID Ownership

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

But this command fails.

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

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

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

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