Bash脚本进程名称正则表达式

Ste*_*eve 4 regex unix bash grep

我正在尝试基于进程名称的部分进行正则表达式进程id.它似乎工作,如果我只做一个单词,但它失败了,当我尝试做类似的事情:找到任何进程与路径/开始**/endswiththis /

这是我到目前为止所拥有的:

QUEUE_PID="$(ps -ef | grep endswiththis | grep -v $0 | grep -v grep | awk '{ print $2 }')";   
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?谢谢,史蒂夫

Bea*_*ano 5

许多UNIX现在都具有pgrep您想要的功能

DESCRIPTION
   pgrep  looks  through the currently running processes and lists the process IDs which
   matches the selection criteria to stdout.  All the criteria have to match.
Run Code Online (Sandbox Code Playgroud)

举个例子:

$ps -ef | grep sendmail
simonp    6004 27310  0 09:16 pts/5    00:00:00 grep sendmail
root      6800     1  0 Jul19 ?        00:00:03 sendmail: accepting connections
smmsp     6809     1  0 Jul19 ?        00:00:01 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue

$pgrep sendmail
6800
6809
Run Code Online (Sandbox Code Playgroud)

传递给的参数pgrep是一个正则表达式 - 它与可执行文件名或依赖于参数(-f)的完整进程参数字符串相匹配.

$pgrep  '^sen.*il$'
6800
6809

$pgrep -f '^sendmail.*connections$'
6800
Run Code Online (Sandbox Code Playgroud)

欲获得更多信息

man pgrep
Run Code Online (Sandbox Code Playgroud)