bash 如何搜索PID?(重击)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/905242/
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
How to do a search for PID? (bash)
提问by Herves
assuming that I know the PID of a process and want to do a search in ps -A, how do I do it? I tried doing this:
假设我知道进程的 PID 并想在 ps -A 中进行搜索,我该怎么做?我试过这样做:
echo "Enter PID to search: "
read PID
search=$(ps -A | grep -v PID | awk '{print }')
This returns me with a long list of PIDs. So how can I get use each individual value of the output and do:
这会返回一长串PID。那么我如何才能使用输出的每个单独值并执行以下操作:
if [ "$PID" = "*each_value_in_search_list*" ]; then
........
In this case i'm trying to compare what the user enters with the output of my command, so how do I do it? Am I doing the correct way in the first place? Or is there any other way to do this?
在这种情况下,我试图将用户输入的内容与我的命令的输出进行比较,那么我该怎么做呢?我首先做的是正确的方法吗?或者有没有其他方法可以做到这一点?
Thanks for your help, everyone who answered this question. (:
感谢所有回答这个问题的人的帮助。(:
回答by John T
The -v switch for grep performs an inverted search, in other words you will get everything you DON'T want. After a variable is set, you should also reference it prefixed with $.
grep 的 -v 开关执行反向搜索,换句话说,您将获得不需要的所有内容。设置变量后,您还应该引用它以 $ 为前缀。
try this
尝试这个
#!/bin/bash
echo "Enter PID to search: "
read PID
search=$(ps --pid $PID -o comm=)
if [ $search ]
then
echo "Program: $search"
else
echo "No program found with PID: $PID"
fi
回答by paxdiablo
Your "grep -v PID"is a standard trick to remove the heading line, not a way to get the actual PID details from the output.
您"grep -v PID"是删除标题行的标准技巧,而不是从输出中获取实际 PID 详细信息的方法。
If all you want is the details for that process, just use:
如果您想要的只是该过程的详细信息,只需使用:
search=$(ps -A | awk -v pid=$PID '==pid{print pid}')
That will set search to the PID in question if it exists or the empty string otherwise, which is what you appear to want.
如果存在问题,则将搜索设置为有问题的 PID,否则设置为空字符串,这正是您想要的。
There's a problem with doing a blind search for the PID (as opposed to checking column 1 with awk) is that you may pick up lines where that PID shows up in the command string.
对 PID 进行盲搜索(而不是使用 来检查第 1 列awk)存在一个问题,即您可能会选择该 PID 出现在命令字符串中的行。
回答by Adam Rosenfield
You're grepping the output of psfor all lines which do not contain the literal string "PID". You want to use $PIDinstead to use the variablenamed PID instead of the literal "PID". Furthermore, since you do not want false positives on other fields matching the PID, just match on the first column:
您正在搜索ps不包含文字字符串“PID”的所有行的输出。您想改用名为 PID$PID的变量而不是文字“PID”。此外,由于您不希望与 PID 匹配的其他字段出现误报,只需匹配第一列:
search=$(ps -A | grep "^ *$PID\>" | awk '{print }')
The "\>"escape sequence matches the empty string at the end of a word.
该"\>"转义序列在单词的末尾的空字符串匹配。
Some versions of pssupport a -poption to only give you the info about a specific PID, so you don't need grep or awk:
某些版本ps支持一个-p选项,只为您提供有关特定 PID 的信息,因此您不需要 grep 或 awk:
search=$(ps -p $PID)
回答by Dave Webb
Instead of using grepyou could use the -poption to pass the PID directly to the pscommand.
grep您可以使用-p选项将 PID 直接传递给ps命令,而不是使用。
回答by TheBonsai
If the root of the question is to check if the entered PID corresponds to a process entry (root). More specific, if you are able to send a signal to that process (user):
如果问题的根源是检查输入的PID是否对应于进程条目(root)。更具体地说,如果您能够向该进程(用户)发送信号:
if kill -0 $PID >/dev/null 2>&1; then ...
It could be, of course, that this is not at all what you want. I felt free to "interpret" your question a bit ;)
当然,这可能根本不是您想要的。我可以随意“解释”您的问题;)
回答by diyism
<?php
echo shell_exec("ps -Ao cmd,pid|grep -P ' {$pid}$'");
?>

