为了账号安全,请及时绑定邮箱和手机立即绑定

更加优雅的“ ps aux | grep -v grep”

更加优雅的“ ps aux | grep -v grep”

尚方宝剑之说 2019-10-24 11:17:39
当我检查过程列表并“ grep”出对我来说很有趣的过程时,其grep本身也包含在结果中。例如,列出终端:$ ps aux  | grep terminaluser  2064  0.0  0.6 181452 26460 ?        Sl   Feb13   5:41 gnome-terminal --working-directory=..user  2979  0.0  0.0   4192   796 pts/3    S+   11:07   0:00 grep --color=auto terminal通常我ps aux | grep something | grep -v grep用来摆脱最后一个条目...但是它并不优雅 :)您是否有更优雅的技巧来解决此问题(将所有命令包装到单独的脚本中,这也不错)
查看完整描述

3 回答

?
拉风的咖菲猫

TA贡献1995条经验 获得超2个赞

通常的技术是这样的:


ps aux | egrep '[t]erminal'

这将匹配包含的行terminal,但egrep '[t]erminal'不匹配!它也可以在许多 Unix版本上使用。


查看完整回答
反对 回复 2019-10-24
?
交互式爱情

TA贡献1712条经验 获得超3个赞

该答案基于先前的pgrep 答案。它还建立在另一个答案结合使用的ps与pgrep。以下是一些相关的培训示例:


$ pgrep -lf sshd

1902 sshd


$ pgrep -f sshd

1902


$ ps up $(pgrep -f sshd)

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

root      1902  0.0  0.1  82560  3580 ?        Ss   Oct20   0:00 /usr/sbin/sshd -D


$ ps up $(pgrep -f sshddd)

error: list of process IDs must follow p

[stderr output truncated]


$ ps up $(pgrep -f sshddd) 2>&-

[no output]

以上可以用作功能:


$ psgrep() { ps up $(pgrep -f $@) 2>&-; }


$ psgrep sshd

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

root      1902  0.0  0.1  82560  3580 ?        Ss   Oct20   0:00 /usr/sbin/sshd -D

用比较ps有grep。不会打印出有用的标题行:


$  ps aux | grep [s]shd

root      1902  0.0  0.1  82560  3580 ?        Ss   Oct20   0:00 /usr/sbin/sshd -D


查看完整回答
反对 回复 2019-10-24
?
慕娘9325324

TA贡献1783条经验 获得超4个赞

另一种选择:


ps -fC terminal

这里的选项:


 -f        does full-format listing. This option can be combined

           with many other UNIX-style options to add additional

           columns. It also causes the command arguments to be

           printed. When used with -L, the NLWP (number of

           threads) and LWP (thread ID) columns will be added. See

           the c option, the format keyword args, and the format

           keyword comm.


 -C cmdlist     Select by command name.

                This selects the processes whose executable name is

                given in cmdlist.


查看完整回答
反对 回复 2019-10-24
  • 3 回答
  • 0 关注
  • 1248 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信