Using pidof and pgrep to list process IDs

The pidof and pgrep commands make selecting and using process IDs on the Linux command line painless.

Number 10, distressed type
Shelly Still / Getty Images

ThePIDOF.andpgrepcommands 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 calledPIDOF.。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 thepscommand 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 CMD1262005pts/0 00:00:00 bash

If more than one person is logged in and using抨击, the response to that command will includeall对于BASH进程的关联PID,您不一定知道没有一些PID属于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 likesystemd, you could run a command like this one:

$ pidof systemd 1265265 1261815 1548 1

Notice that the output shows that foursystemdprocesses are running. To display just one PID, you can add the-soption, but thePIDOF.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 usePIDOF., the process you are asking about either isn’t running or you mistyped its name.

$ pidof apache2 $

Interestingly, if you do a file listing forPIDOF., you’ll see that it’s nothing more than a symbolic link to another program,killall5on 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 calledPIDOF.。When invoked withkillall5, the command is one of a number of commands that is used to terminate processes. When referred to asPIDOF., it returns PIDs.

pgrep

Thepgrepcommand works a lot likePIDOF., but there are some differences both in the content and the arrangement of its output. For example, when we usePIDOF.to get a list of PIDs forsystemd, we get a list like this one:

$ pidof systemd 1261815 1548 1

When we usepgrep另一方面,我们获得PID的垂直列表,并查看更多条目。为什么?因为pgrep行为更像grep。它寻找包含作为参数提供的字符串的进程,而不仅仅是那些符合进程名称的字符串。

$ 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 thePIDOF.andpgrepcommands 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:

Join the Network World communities onFacebookandLinkedInto comment on topics that are top of mind.
Related:

Copyright © 2020Raybet2

IT Salary Survey:结果是在