linux命令ps aux grep,更优雅的“ps aux |” grep -v grep“

通常的技术是这样的:

ps aux | egrep '[t]erminal'

这将匹配包含terminal ,其中egrep '[t]erminal'不! 它也适用于Unix的许多口味。

使用pgrep 。 这更可靠。

这个答案build立在一个先前的pgrep 答案 。 它也build立在另一个结合使用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]

以上可以作为一个function使用:

$ 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

你可以在ps命令中进行过滤,例如

ps u -C gnome-terminal

(或通过查找 / proc 查找等)

另一个select :

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.

使用括号来包围search模式中的字符,排除了grep进程,因为它不包含匹配的正则expression式。

$ ps ax | grep 'syslogd' 16 ?? Ss 0:09.43 /usr/sbin/syslogd 18108 s001 S+ 0:00.00 grep syslogd $ ps ax | grep '[s]yslogd' 16 ?? Ss 0:09.43 /usr/sbin/syslogd $ ps ax | grep '[s]yslogd|grep' 16 ?? Ss 0:09.43 /usr/sbin/syslogd 18144 s001 S+ 0:00.00 grep [s]yslogd|grep

免责声明:我是这个工具的作者,但…

我会用px :

~ $ px atom PID COMMAND USERNAME CPU RAM COMMANDLINE 14321 crashpad_handler walles 0.01s 0% /Users/walles/Downloads/Atom.app/Contents/Frameworks/Electron Framework.framework/Resources/crashpad_handler --database= 16575 crashpad_handler walles 0.01s 0% /Users/walles/Downloads/Atom.app/Contents/Frameworks/Electron Framework.framework/Resources/crashpad_handler --database= 16573 Atom Helper walles 0.5s 0% /Users/walles/Downloads/Atom.app/Contents/Frameworks/Atom Helper.app/Contents/MacOS/Atom Helper --type=gpu-process --cha 16569 Atom walles 2.84s 1% /Users/walles/Downloads/Atom.app/Contents/MacOS/Atom --executed-from=/Users/walles/src/goworkspace/src/github.com/github 16591 Atom Helper walles 7.96s 2% /Users/walles/Downloads/Atom.app/Contents/Frameworks/Atom Helper.app/Contents/MacOS/Atom Helper --type=renderer --no-san

除了使用明智的命令行界面来查找进程之外,它还做了很多其他有用的事情, 在项目页面上有更多的细节。

适用于Linux和OS X,轻松安装:

curl -Ls https://github.com/walles/px/raw/python/install.sh | bash

另一个select是编辑你的.bash_profile (或其他你保存bash别名的文件)来创build一个greps'grep'结果的函数。

function mygrep { grep -v grep | grep --color=auto $1 } alias grep='mygrep'

grep -v grep必须是第一的,否则你的--color=auto将不能工作。

如果你使用bash的话,这将起作用。 如果你使用不同的shell YMMV。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值