首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Linux命令- 'ps‘

Linux命令- 'ps‘
EN

Stack Overflow用户
提问于 2013-12-13 20:05:53
回答 2查看 4.1K关注 0票数 3

我的目标是找到具有高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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-13 20:08:57

为一个程序提供一个参数和编写一个程序的标准输入是有区别的,您正在这样做。

在第一种情况下,程序将参数列表读取为字符串数组,这些字符串可以由程序解释。在第二种情况下,程序本质上是从一个特殊的文件中读取并处理其内容。你在程序名称后面放的所有东西都是参数。ps需要许多可能的参数,例如-p和进程的PID。在您的命令中,您不提供PID作为参数,而是写到ps的stdin,它会忽略它。

但是您可以使用xargs,它读取其标准输入并将其用作命令的参数:

代码语言:javascript
复制
ps -ef | cut -d " " -f 6 | sort | tail -n1 | xargs ps -p

这就是xargs所做的(来自man):

代码语言:javascript
复制
xargs - build and execute command lines from standard input

也可以使用命令替换,如janos所示。在本例中,shell将$()中的表达式计算为命令,并将其输出改为。因此,在展开之后,您的命令看起来像ps -p 12345

man bash

代码语言:javascript
复制
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).
票数 4
EN

Stack Overflow用户

发布于 2013-12-13 20:09:26

也许你在找这个

代码语言:javascript
复制
ps -p $(ps -ef | cut -d " " -f 6 | sort | tail -n 1)

也就是说,ps -p PID打印命令行上指定的PID的详细信息。它不能从标准输入中提取其参数。

或者您可以使用xargs,如Lev Levitsky所示;-)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20575044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档