我想使用 ps -ef | grep "keyword"
确定一个守护进程的pid(在其中ps -ef的输出中有一个唯一的字符串)。
我可以用 pkill 关键字
杀死进程 有没有返回 pid 而不是杀死它的命令? (pidof 或 pgrep 不起作用)
最佳答案
只要包含 -f
选项,您可以使用 pgrep
。这使得 pgrep
匹配整个命令(包括参数)中的关键字,而不仅仅是进程名称。
pgrep -f keyword
来自 man page :
-f
The pattern is normally only matched against the process name. When-f
is set, the full command line is used.
如果你真的想避免 pgrep,试试:
ps -ef | awk '/[k]eyword/{print $2}'
注意关键字第一个字母周围的[]
。这是避免匹配 awk
命令本身的有用技巧。
https://stackoverflow.com/questions/8120304/