我的目标是找到具有高PID的进程(是的,我知道可以只执行ps -ef|tail -n 1,但我想先找到PID,然后找到进程),所以我使用了下面的命令--使用最高级的PID查找进程:ps -ef|cut -d " " -f 6|sort|tail -n 1,然后找到获得最高PID并输出匹配进程的ps -p (当我手动复制PID时,这是有效的),但是由于某种原因,当我在它们之间放置'|‘时,会出现语法错误。有人能指出问题是什么吗?另外,如果你有更好的方式发布这件事。
Tnx,Dean
ps,不起作用的完整命令是:ps -ef|cut -d " " -f 6|sort|tail -n 1|ps -p。
发布于 2013-12-13 20:08:57
为一个程序提供一个参数和编写一个程序的标准输入是有区别的,您正在这样做。
在第一种情况下,程序将参数列表读取为字符串数组,这些字符串可以由程序解释。在第二种情况下,程序本质上是从一个特殊的文件中读取并处理其内容。你在程序名称后面放的所有东西都是参数。ps需要许多可能的参数,例如-p和进程的PID。在您的命令中,您不提供PID作为参数,而是写到ps的stdin,它会忽略它。
但是您可以使用xargs,它读取其标准输入并将其用作命令的参数:
ps -ef | cut -d " " -f 6 | sort | tail -n1 | xargs ps -p这就是xargs所做的(来自man):
xargs - build and execute command lines from standard input也可以使用命令替换,如janos所示。在本例中,shell将$()中的表达式计算为命令,并将其输出改为。因此,在展开之后,您的命令看起来像ps -p 12345。
man bash
Command Substitution
Command substitution allows the output of a command to replace the com‐
mand name. There are two forms:
$(command)
or
`command`
Bash performs the expansion by executing command and replacing the com‐
mand substitution with the standard output of the command, with any
trailing newlines deleted. Embedded newlines are not deleted, but they
may be removed during word splitting. The command substitution $(cat
file) can be replaced by the equivalent but faster $(< file).发布于 2013-12-13 20:09:26
也许你在找这个
ps -p $(ps -ef | cut -d " " -f 6 | sort | tail -n 1)也就是说,ps -p PID打印命令行上指定的PID的详细信息。它不能从标准输入中提取其参数。
或者您可以使用xargs,如Lev Levitsky所示;-)
https://stackoverflow.com/questions/20575044
复制相似问题