Linux Comm Pgrep
[ Linux Command Encyclopaedia](#)\n\n* * *\n\npgrep is a very practical process search tool in Linux systems, and it is part of the procps or procps-ng software package. The name of this command comes from "process grep", as the name suggests, it can search for processes in the system like the grep command searches for text.\n\nThe main function of pgrep is to find running processes based on given conditions and return the PIDs (Process IDs) of these processes. Compared with the traditional method of combining ps command with grep, pgrep is more concise and efficient.\n\n* * *\n\n## pgrep Basic Syntax\n\nThe basic syntax format of pgrep command is as follows:\n\npgrep \nWhere:\n\n* `options`: Various parameters used to specify search conditions or output format\n* `pattern`: Regular expression pattern used to match process names\n\n* * *\n\n## Common Option Parameters\n\npgrep provides multiple options to precisely control search behavior. Here are the commonly used options:\n\n| Option | Description |\n| --- | --- |\n| `-l` | Display both process name and PID |\n| `-a` | Display full command line rather than just process name |\n| `-f` | Match full command line (including arguments) rather than just process name |\n| `-u` | Only match processes owned by specified user |\n| `-x` | Exactly match the entire process name |\n| `-n` | Only display the newest (most recently started) matching process |\n| `-o` | Only display the oldest (earliest started) matching process |\n| `-c` | Only return the count of matching processes without displaying PIDs |\n| `-d` | Specify output delimiter (default is newline) |\n| `-P` | Only match child processes of specified parent process ID |\n| `-g` | Only match processes in specified process group ID |\n| `-t` | Only match processes on specified terminal (tty) |\n\n* * *\n\n## Usage Examples\n\n### Basic Process Search\n\nFind all processes named "nginx":\n\npgrep nginx\nThis command will return all process IDs whose process name contains "nginx".\n\n### Display Process Name\n\nFind "ssh" process and display process name:\n\npgrep -l ssh\nExample output:\n\n1234 sshd 5678 ssh-agent\n### Exact Process Name Match\n\nOnly match processes with exactly the name "bash" (not including "bashrc", etc.):\n\npgrep -x bash\n### Match Full Command Line\n\nFind processes containing specific parameters, for example, find sshd process using a specific port:\n\npgrep -f "sshd -p 2222"\n### Filter by User\n\nFind all processes running by user "www-data":\n\npgrep -u www-data\n### Combined Condition Search\n\nFind processes named "mysqld" running by user "mysql":\n\npgrep -u mysql mysqld\n### Count Process Number\n\nCount the number of running "chrome" processes:\n\npgrep -c chrome\n### Find Newest/Oldest Process\n\nFind the newest started "python" process:\n\npgrep -n python\nFind the oldest started "python" process:\n\npgrep -o python\n\n* * *\n\n## Advanced Usage\n\n### Use with pkill\n\npgrep is often used together with pkill command. You can first use pgrep to see which processes will be terminated, and then use pkill after confirmation:\n\n## Example\n\n# First view matching processes\n\n pgrep -l apache2\n\n# After confirmation, terminate these processes\n\n pkill apache2\n\n### Use Custom Delimiter\n\nBy default, pgrep uses newline to separate multiple PIDs. You can change to use comma instead:\n\npgrep -d, nginx\nExample output:\n\n1234,5678,9012\n### Find Processes on Specific Terminal\n\nFind all processes running on tty1 terminal:\n\npgrep -t tty1\n### Find Child Processes\n\nFind all child processes of process with PID 1234:\n\npgrep -P 1234\n\n* * *\n\n## FAQ\n\n### What is the difference between pgrep and ps | grep?\n\n1. **Efficiency**: pgrep directly queries kernel process information, more efficient than ps | grep\n2. **Security**: pgrep won't match its own process, while ps | grep might match the grep process itself\n3. **Functionality**: pgrep provides more specialized process search options\n\n### Why can't pgrep find a process I know is running?\n\nPossible reasons:\n\n1. Process name spelling error\n2. Not using `-f` option to match full command line\n3. Process belongs to another user, and you don't have sufficient permissions to view it\n4. Process has already terminated\n\n### How to check pgrep version?\n\npgrep -V\n\n* * *\n\n## Practical Application Scenarios\n\n### Monitor Service Status\n\nCheck if a service is running in a script:\n\n## Example\n\nif pgrep -x"nginx">/dev/null; then\n\necho"Nginx is running"\n\nelse\n\necho"Nginx is not running"\n\nfi\n\n### Batch Operation on Processes\n\nGet PIDs of all Java processes and perform operations:\n\n## Example\n\nfor pid in $(pgrep java); do\n\necho"Processing Java process $pid"\n\n# Other operations...\n\ndone\n\n### System Resource Monitoring\n\nCount the number and resource usage of specific type of processes:\n\n## Example\n\ncount=$(pgrep -c chrome)\n\nmemory=$(ps-o rss= -p $(pgrep chrome)|awk'{sum+=$1} END {print sum}')\n\necho"Chrome processes: $count, Total memory: ${memory}KB"\n\n* * *\n\n## Summary\n\npgrep is a powerful and efficient tool in Linux system management, which simplifies process search and management operations. By mastering the various options and usage of pgrep, you can:\n\n1. Quickly and accurately locate specific processes\n2. Write more reliable system management scripts\n3. Improve efficiency of daily system maintenance work\n\nRemember, after using pgrep to find processes, especially when preparing to terminate processes, be sure to confirm that you have found the correct processes to avoid accidentally terminating important services.\n\n* * Linux Command Encyclopaedia](#)
YouTip