ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Linux命令 – ‘ps’

2019-07-28 19:01:07  阅读:187  来源: 互联网

标签:bash linux shell putty ps


我的目标是用高斯PID找到进程(是的,我知道可以只做ps -ef | tail -n 1,但我想首先找到PID然后找到进程),所以我用下面的命令找到了使用最高PID的过程:
 ps -ef | cut -d“” – f 6 | sort | tail -n 1
然后我发现ps -p获得最高的PID并输出匹配过程(当我手动复制PID时有效)但出于某种原因,当我把’|’他们之间说语法错误.有谁可以指出问题是什么?
如果你有更好的方法来发布这个东西.

TNX,
院长

ps,不起作用的完整命令是:
ps -ef | cut -d“” – f 6 | sort | tail -n 1 | ps -p.

解决方法:

为程序提供参数和写入程序的标准输入之间存在差异.

在第一种情况下,程序将参数列表作为字符串数组读取,可以由程序解释.在第二种情况下,程序基本上从特殊文件中读取并处理其内容.你在程序名后面放的所有内容都是参数. 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.

男子打击:

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

标签:bash,linux,shell,putty,ps
来源: https://codeday.me/bug/20190728/1564479.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有