Linux 从 ps -ef |grep 关键字获取 pid
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8120304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting pids from ps -ef |grep keyword
提问by Dennis Ich
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).
我想用它ps -ef | grep "keyword"
来确定守护进程的 pid(其中 ps -ef 的输出中有一个唯一的字符串)。
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)
我可以用pkill keyword
是否有任何命令返回 pid 而不是杀死它来终止进程?(pidof 或 pgrep 不起作用)
采纳答案by Shawn Chin
You canuse 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
pgrep
pgrep -f keyword
From the man page:
从手册页:
-f
The pattern is normally only matched against the process name. When-f
is set, the full command line is used.
-f
该模式通常仅与进程名称匹配。时-f
被设定,则使用完整的命令行。
If you really want to avoid pgrep, try:
如果您真的想避免使用 pgrep,请尝试:
ps -ef | awk '/[k]eyword/{print }'
Note the []
around the first letter of the keyword. That's a useful trick to avoid matching the awk
command itself.
注意[]
关键字的第一个字母。这是避免匹配awk
命令本身的有用技巧。
回答by Lewis Norton
Try
尝试
ps -ef | grep "KEYWORD" | awk '{print $2}'
ps -ef | grep "KEYWORD" | awk '{print $2}'
That command should give you the PID of the processes with KEYWORD in them. In this instance, awk
is returning what is in the 2nd column from the output.
该命令应该为您提供其中包含 KEYWORD 的进程的 PID。在这种情况下,awk
从输出返回第二列中的内容。
回答by dbrank0
This is available on linux: pidof keyword
这在 linux 上可用:pidof 关键字
回答by Vinayak
ps -ef | grep KEYWORD | grep -v grep | awk '{print $2}'
ps -ef | grep KEYWORD | grep -v grep | awk '{print $2}'
回答by Arksonic
I use
我用
ps -C "keyword" -o pid=
This command should give you a PID number.
这个命令应该给你一个PID号。
回答by swayamraina
To kill a process by a specific keyword you could create an alias in ~/.bashrc
(linux) or ~/.bash_profile
(mac).
要通过特定关键字杀死进程,您可以在~/.bashrc
(linux) 或~/.bash_profile
(mac) 中创建别名。
alias killps="kill -9 `ps -ef | grep '[k]eyword' | awk '{print }'`"