大网数据广告活动
服务器运维问答

pgrep search multiple words

· 52CE 我爱测

I'm using pgrep to find running processes having some words > pgrep -f "otp" 2345 2343 More than one process is identified. There is another word in the command line that would help me zero in to the exact process. So, I want to …

问题原文

原帖链接 · Nikhil VJ · CC BY-SA 4.0

I'm using pgrep to find running processes having some words

> pgrep -f "otp"
2345
2343

More than one process is identified. There is another word in the command line that would help me zero in to the exact process. So, I want to search for two terms and both should be there. Unfortunately I'm only seeing examples and help for OR combinations like this:

> pgrep -f "otp|place1"
2345
2343
3632

That does not work because it's an OR condition. I need an AND condition in my search pattern.

pgrep reference: https://linux.die.net/man/1/pgrep

pattern: Specifies an Extended Regular Expression for matching against the process names or command lines.

How do I use pgrep to search for two words in AND combination instead of OR?

社区高分解答

原帖链接 · Server Fault · CC BY-SA 4.0

I gave up on pgrep long ago. Always seemed like it should be more useful that what it ends up being.

This is what I use. A bit long winded, and always thought there must be a better way, so maybe someone can enlighten us both.

egrep allows for filtering with extended regular expressions which becomes important when you only want "one answer". grep -E is supposed to do the same thing but I got in the habit of using egrep because I wanted a solution which worked across multiple flavors of *nix.

The -e flag tells ps to show all processes (runs with permission of executing user. ie: not all procs show unless you are root). The-f flag tells ps to issue a "full-format" listing which you might need to | less when viewing it since longer CLI args might scroll off the right of the terminal and might not wrap. Hope it helps.

Find processes matching agetty:

# ps -ef | egrep agetty
root      1386     1  0 Sep19 ttyS0    00:00:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220
root      1388     1  0 Sep19 tty1     00:00:00 /sbin/agetty --noclear tty1 linux
root     28332  4228  0 11:23 pts/2    00:00:00 grep -E agetty

Find process matching agetty that also have baud in them:

# ps -ef | egrep 'agetty.*baud'
root      1386     1  0 Sep19 ttyS0    00:00:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220
root     28347  4228  0 11:23 pts/2    00:00:00 grep -E agetty.*baud

Don't return my grep process. Not sure how it works but saves you from having to | grep -v 'grep' on the results. Maybe someone could explain this:

# ps -ef | egrep 'agetty.*b[a]ud'
root      1386     1  0 Sep19 ttyS0    00:00:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220

egrep with the OR condition

# ps -ef | egrep '(ag[e]tty|ac[p]id)'
root       944     1  0 Sep19 ?        00:00:00 /usr/sbin/acpid
root      1386     1  0 Sep19 ttyS0    00:00:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220
root      1388     1  0 Sep19 tty1     00:00:00 /sbin/agetty --noclear tty1 linux
root     28395  4228  0 11:28 pts/2    00:00:00 grep -E (agetty|acpid)

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-09-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。