The pidof and pgrep instructions present listings of course of IDs (PIDs) for course of names that you just present as arguments. This submit reveals how you can use these instructions and illustrates the variations between them with a sequence of examples.
pidof
There are a selection of how to find out the PID of a course of operating on a Linux system, however the best might be a command known as pidof. Learn this as “PID of” and also you’ll have a simple time remembering it. Utilizing this command, you will get the PID of a course of by typing “pidof” and specifying the method title. For instance:
$ pidof bash 1262005
Should you had been to run the ps command with out arguments, you’re going to get an inventory of the processes that you just’re operating in your present shell. The command beneath reveals the place the response above comes from:
$ ps PID TTY TIME CMD 1262005 pts/0 00:00:00 bash
If multiple individual is logged in and utilizing bash, the response to that command will embody all of the related PIDs for bash processes, and also you gained’t essentially know with out some further instructions which PID belongs to your shell:
$ pidof bash 1265446 1262005
You might run a command like this to indicate simply your shell:
$ ps | grep bash | awk ‘print $1’
1262005
To get a list of PIDs for system processes like systemd, you could possibly run a command like this one:
$ pidof systemd 1265265 1261815 1548 1
Discover that the output reveals that 4 systemd processes are operating. To show only one PID, you may add the -s possibility, however the pidof command will solely then be offering the biggest (extra lately began) PID within the group.
$ pidof -s systemd 1265265
Should you get no response if you use pidof, the method you’re asking about both isn’t operating otherwise you mistyped its title.
$ pidof apache2 $
Curiously, in the event you do a file itemizing for pidof, you’ll see that it’s nothing greater than a symbolic hyperlink to a different program, killall5 on this method.
$ 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 in another way when it’s known as pidof. When invoked with killall5, the command is one among various instructions that’s used to terminate processes. When known as pidof, it returns PIDs.
pgrep
The pgrep command works loads like pidof, however there are some variations each within the content material and the association of its output. For instance, once we use pidof to get an inventory of PIDs for systemd, we get an inventory like this one:
$ pidof systemd 1261815 1548 1
Once we use pgrep, however, we get a vertical itemizing of the PIDs and see extra entries. Why? As a result of pgrep acts extra like grep. It seems to be for processes that comprise the string that’s supplied as an argument, not simply people who match the method title precisely.
$ pgrep systemd 1 1010 1548 171864 171907 172061 1261815
Within the command beneath, you may see extra particulars on the processes that the above command chosen:
$ 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
Deciding on course of IDs might be very helpful at occasions, and the pidof and pgrep instructions preserve you from having to pipe the output of instructions to different instructions to slim down the output to an inventory containing solely course of IDs.
Now see:
Copyright © 2020 IDG Communications, Inc.
Leave a Reply