从ps -ef | grep关键字获取pid [英] Getting pids from ps -ef |grep keyword

查看:1833
本文介绍了从ps -ef | grep关键字获取pid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用ps -ef | grep "keyword"确定守护进程的pid(其中ps -ef的输出中有一个唯一的字符串).

I want to use ps -ef | grep "keyword" to determine the pid of a daemon process (there is a unique string in output of ps -ef in it).

我可以使用pkill keyword杀死进程,是否有任何命令返回pid而不是杀死pid? (pidof或pgrep无效)

I can kill the process with pkill keyword is there any command that returns the pid instead of killing it? (pidof or pgrep doesnt work)

推荐答案

只要包含-f选项,您可以使用pgrep.这使得pgrep与整个命令(包括参数)中的关键字匹配,而不仅仅是过程名称.

You can use pgrep as long as you include the -f options. That makes pgrep match keywords in the whole command (including arguments) instead of just the process name.

pgrep -f keyword

手册页中:

-f     该模式通常仅与进程名称匹配.设置-f时,将使用完整的命令行.

-f       The pattern is normally only matched against the process name. When -f is set, the full command line is used.


如果您真的想避免使用pgrep,请尝试:


If you really want to avoid pgrep, try:

ps -ef | awk '/[k]eyword/{print $2}'

请注意关键字首字母附近的[].这是避免与awk命令本身匹配的有用技巧.

Note the [] around the first letter of the keyword. That's a useful trick to avoid matching the awk command itself.

这篇关于从ps -ef | grep关键字获取pid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆