- Suspected Phobos Ransomware Admin Extradited to US
- Luz verde al nuevo laboratorio de innovación en APIs para Open Gateway
- Cheap MacBooks vs. Android Laptops: Google's plan to make desktop Linux a reality
- The Future of Cybersecurity: Why Vendor Consolidation is the Next Big
- 기고 | 비즈니스를 강화하는 내부용 API를 설계하는 법
Using pidof and pgrep to list process IDs
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:
$ pidof systemd 1265265 1261815 1548 1
Notice that the output shows that four systemd processes are running. To display just one PID, you can add the -s option, but the pidof command will only then be providing the largest (more recently started) PID in the group.
$ pidof -s systemd 1265265
If you get no response when you use pidof, the process you are asking about either isn’t running or you mistyped its name.
$ pidof apache2 $
Interestingly, if you do a file listing for pidof, you’ll see that it’s nothing more than a symbolic link to another program, killall5 on this system.
$ which pidof /bin/pidof $ ls -l /bin/pidof lrwxrwxrwx 1 root root 14 Feb 13 2020 /bin/pidof -> /sbin/killall5
Even so, the executable behaves very differently when it’s called pidof. When invoked with killall5, the command is one of a number of commands that is used to terminate processes. When referred to as pidof, it returns PIDs.
pgrep
The pgrep command works a lot like pidof, but there are some differences both in the content and the arrangement of its output. For example, when we use pidof to get a list of PIDs for systemd, we get a list like this one:
$ pidof systemd 1261815 1548 1
When we use pgrep, on the other hand, we get a vertical listing of the PIDs and see more entries. Why? Because pgrep acts more like grep. It looks for processes that contain the string that is provided as an argument, not just those that match the process name exactly.
$ pgrep systemd 1 1010 1548 171864 171907 172061 1261815
In the command below, you can see more details on the processes that the above command selected:
$ ps -eo pid,euser,comm | grep systemd 1 root systemd 1010 root systemd-logind 1548 gdm systemd 171864 systemd+ systemd-resolve 171907 root systemd-journal 172061 root systemd-udevd 1261815 shs system
Wrap-Up
Selecting process IDs can be very useful at times, and the pidof and pgrep commands keep you from having to pipe the output of commands to other commands to narrow down the output to a list containing only process IDs.
Now see:
Copyright © 2020 IDG Communications, Inc.