The pidof and pgrep commands provide listings of process IDs (PIDs) for process names that you provide as arguments. This post shows how to use these commands and illustrates the differences between them with a series of examples.
pidof
There are a number of ways to determine the PID of a process running on a Linux system, but the easiest is probably a command called pidof. Read this as “PID of” and you’ll have an easy time remembering it. Using this command, you can get the PID of a process by typing “pidof” and specifying the process name. For example:
$ pidof bash 1262005
If you were to run the ps command without arguments, you will get a list of the processes that you’re running in your current shell. The command below shows where the response above comes from:
$ ps PID TTY TIME CMD 1262005 pts/0 00:00:00 bash
If more than one person is logged in and using bash, the response to that command will include all of the associated PIDs for bash processes, and you won’t necessarily know without some additional commands which PID belongs to your shell:
$ pidof bash 1265446 1262005
You could run a command like this to show just your shell:
$ ps | grep bash | awk ‘{print $1}’
1262005
To get a listing of PIDs for system processes like systemd, you could run a command like this one:
Copyright © 2020 IDG Communications, Inc.