Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Pgrep Command in Linux - DesignLinux

Sep 30 2020
designlinux 0 Comments
Pgrep Command in Linux

The pgrep is a command-line utility used to find the process IDs of a running application. It’s inlcuded in procps package and pre-installed on most of the Linux distributions. The process name can be full or partial, which running by a user or other attributes. This article we will discuss the basics of the Linux pgrep command.

Use pgrep Command#

Following is the basic syntax of the pgrep command:

pgrep [OPTIONS] <PATTERN>

Once you run the pgrep command without any options, it will show the PIDs of all running programs which matches with given names.

For example, to get the PID of the Nginx server, you would run:

pgrep nginx

If the processes found with the matching name “nginx”, it show the PIDs of the processes. Otherwise it will not show any output.

23101
23104

If any one running process matches with the given name it will returns 0, otherwise the exit code is 1. It can be useful while writing shell scripts.

If you want to send signals to the matched processes use pkill . This uses same options and pattern matching.

By default the pgrep command will print the each PID on a newline. You can change the delimiter to separate using the -d option.

For example, if you want to use a pipe sign as a delimiter, enter:

pgrep nginx -d'|'
23101|23104

The -l option tells pgrep to show the process name along with its ID:

pgrep nginx -l
23101 nginx
23104 nginx

You also can use the regular expression with pgrep command to do search operation and it will list all the processes that having the name “nginx”.

If you want to match only the processes which names are exactly as the search pattern, you would use:

pgrep '^nginx$' -l
23101 nginx
23104 nginx

The pgrep matches only against the process name, you can use the -f option to matches against full argument lists.

pgrep nginx -f

To get the list of processes of a specific user use -u option with pgrep command:

pgrep -u root

You also can specify the multiple users names with comma separated list:

pgrep -u root,tecnstuff

Conclusion#

The pgrep command is used to get the PIDs of a running program. To know more about pgrep command, take a look at pgrep man page.

If you have any questions or feedback, feel free to leave a comment.

Related

Tags: pgrep, terminal

Stacer – Linux System Optimizer & Monitoring Tool

Prev Post

How I Switched from Windows 10 to Linux Mint

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 259
  • 614,631

DesignLinux.com © All rights reserved

Go to mobile version