pidof is a tool that can be used to find the PID of a process. It can be used to determine the process’s name, its current state, and its parent process. pidof can also be used to determine the PID of a child process if one exists. To use pidof, you first need to install it. You can find pidof on most Linux distributions by running apt-get install pidof . Once you have installed pidof, you can use it to find the PID of a process: $ pidof -p myprocess mypid This will print out the PID of myprocess. If you want to determine the PID of a child process that exists in your system, you need to use another tool such as pgrep : $ pgrep myprocess mypid
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.
RELATED: What Are Unix PIDs and How Do They Work?
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 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.
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.
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 .
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.
RELATED: Why Does Chrome Have So Many Open 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.
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.
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 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.
RELATED: How to Use the grep Command on Linux
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.
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 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.
This time we see three bash processes in the results. The other is being used by mary.
We can string usernames together as a comma-separated list.
And we can ask to see all processes for a specific user.
To see the full command line, use the -a (full list) option.
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:
But this command fails.
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.
It’s a little gotcha to watch out for.