未加星标

Linux pgrep Command Tutorial for Beginners (10 Examples)

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

You might already know about thegrep command in linux, which searches for a pattern, and then prints the matching text in the output. What if the requirement is to apply this kind of processing to fetch select information about processes currently running in the system?

Well, you'll be glad to know that there exists a command line tool - pgrep - that lets you do exactly this. In this tutorial, we will discuss the basics of pgrep using some easy to understand examples. But before we do that, it's worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.

Linux pgrep command

The pgrep command in Linux lets users look up processes based on name and other attributes. Following is its syntax:

pgrep [options] pattern

And here's how the tool's man page explains it:

pgrep looks through the currently running processes and lists the process IDs which match the selection criteria to stdout. All the criteria have to match.

Following are some Q&A-styled examples that should give you a good idea on how the pgrep command works.

Q1. How to find ID of a process owned by a specific user?

This can be done using the -u command line option. For example, to find the ID of the 'gedit' process owned by user 'himanshu', use pgrep in the following way:

pgrep -u himanshu gedit

Here's the output this command produced in my case:


Linux pgrep Command Tutorial for Beginners (10 Examples)
Q2. How to make pgrep print count of matching processes?

In case you want the tool to just print the count, and not the IDs themselves, use the -c command line option. For example, to know the count of processes owned by user 'himanshu,' run the following command:

pgrep -c -u himanshu

Following is the output this command produced on my system:

Q3. How to use a custom delimiter in output?

By default, the process IDs in output are delimited by a newline. However, if you want, you can change the delimiter, something which you can do using the -d command line option.

For example, I wanted to use a colon (:) as a delimiter, so I executed the pgrep command in the following way:

pgrep -u himanshu -d:

And following is the output the command produced:

Q4. How to make pgrep search case insensitive?

By default, the pgrep search is case sensitive. However, if you want, you can make it case insensitive. For this, you have to use the -i command line option.

For example:

pgrep -i gedit

Q5. How to make pgrep list process names as well?

For this, use the -l command line option.

Here's an example:

pgrep -u himanshu -l

Here's an excerpt of the output produced on my system:

1793 systemd 1794 (sd-pam) 1807 gnome-keyring-d 1811 gdm-x-session 1813 Xorg 1817 dbus-daemon 1820 gnome-session-b 1914 ssh-agent 1917 gvfsd 1922 gvfsd-fuse 1925 at-spi-bus-laun 1936 dbus-daemon 1938 at-spi2-registr 1954 gnome-shell 1974 ibus-daemon 1978 ibus-dconf 1980 ibus-x11 1982 ibus-portal 1993 gnome-shell-cal 1999 evolution-sourc 2008 dconf-service 2009 gvfs-udisks2-vo 2012 goa-daemon 2020 gvfs-mtp-volume 2024 gvfs-goa-volume ... ... ...

So you can see that in addition to process IDs, process names were also produced in the output.

Q6. How to make pgrep list full command as well?

In case you want pgrep to display the complete command that was used to launch each process, use the -a command line option.

pgrep -u himanshu -a

1793 /lib/systemd/systemd --user 1794 (sd-pam) 1807 /usr/bin/gnome-keyring-daemon --daemonize --login 1811 /usr/lib/gdm3/gdm-x-session --run-script env GNOME_SHELL_SESSION_MODE=ubuntu gnome-session --session=ubuntu 1813 /usr/lib/xorg/Xorg vt2 -displayfd 3 -auth /run/user/1000/gdm/Xauthority -background none -noreset -keeptty -verbose 3 1817 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only 1820 /usr/lib/gnome-session/gnome-session-binary --session=ubuntu 1914 /usr/bin/ssh-agent /usr/bin/im-launch env GNOME_SHELL_SESSION_MODE=ubuntu gnome-session --session=ubuntu 1917 /usr/lib/gvfs/gvfsd 1922 /usr/lib/gvfs/gvfsd-fuse /run/user/1000/gvfs -f -o big_writes 1925 /usr/lib/at-spi2-core/at-spi-bus-launcher 1936 /usr/bin/dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf --nofork --print-address 3 1938 /usr/lib/at-spi2-core/at-spi2-registryd --use-gnome-session 1954 /usr/bin/gnome-shell 1974 ibus-daemon --xim --panel disable 1978 /usr/lib/ibus/ibus-dconf 1980 /usr/lib/ibus/ibus-x11 --kill-daemon 1982 /usr/lib/ibus/ibus-portal 1993 /usr/lib/gnome-shell/gnome-shell-calendar-server 1999 /usr/lib/evolution/evolution-source-registry 2008 /usr/lib/dconf/dconf-service 2009 /usr/lib/gvfs/gvfs-udisks2-volume-monitor 2012 /usr/lib/gnome-online-accounts/goa-daemon 2020 /usr/lib/gvfs/gvfs-mtp-volume-monitor 2024 /usr/lib/gvfs/gvfs-goa-volume-monitor 2034 /usr/lib/gnome-online-accounts/goa-identity-service 2036 /usr/lib/gvfs/gvfs-gphoto2-volume-monitor 2043 /usr/lib/gvfs/gvfs-afc-volume-monitor Q7. How to make pgrep only display the newest process?

If instead of all processes, you want pgrep to output only the most recent process, then this can be done using the -n command line option.

Here's an example:

pgrep -u himanshu -n -l

And following is the output this command produced:

7163 thunderbird

I can confirm that Thunderbird was indeed the most recent process that was launched by the user 'himanshu.'

Q8. How to make pgrep only display the oldest process?

For this, use the -o command line option.

pgrep -u himanshu -o -l

And here's the output this command produced:

1793 systemd Conclusion

So you can see that pgrep is an extremely helpful command. Once you are done practicing the command line option we've discussed here, you can head to the tool's man page to learn more about it.


Linux pgrep Command Tutorial for Beginners (10 Examples)
About Himanshu Arora

Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu's work has also been featured in some of world's other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

tags: pgrep,usr,command,lib,gvfs,gnome,process,ibus,output,line
分页:12
转载请注明
本文标题:Linux pgrep Command Tutorial for Beginners (10 Examples)
本站链接:https://www.codesec.net/view/597033.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(38)