linux - 从 ps -ef |grep 关键字获取 pid

我想使用 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/

相关文章:

linux - dd:如何计算最佳 block 大小?

linux - grep 的基本表达式不支持\d 吗?

python - 从函数中获取文档字符串

python - 何时在 Python 中使用 %r 而不是 %s?

python - 将 python 列表拆分为其他 "sublists"即较小的列表

python - 将 javadoc 用于 Python 文档

linux - 在 Bash 中,如何在文件的每一行之后添加一个字符串?

linux - 如何通过 * 将包括隐藏文件在内的所有文件移动到父目录中

linux - 如何从脚本编辑/etc/sudoers?

python - 如何为每个子图添加标题