从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.