More elegant “ps aux | grep -v grep”
当我检查进程列表并" grep"列出那些对我来说很有趣的进程时,结果中还包括
1 2 3 | $ ps aux | grep terminal user 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 |
通常我使用
您是否有更优雅的技巧来解决此问题(将所有命令包装到单独的脚本中,这也不错)
通常的技术是这样的:
1 | ps aux | egrep '[t]erminal' |
这将匹配包含
使用pgrep。更可靠。
该答案基于先前的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $ 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] |
以上可以用作功能:
1 2 3 4 5 | $ 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 |
与将
1 2 | $ ps aux | grep [s]shd root 1902 0.0 0.1 82560 3580 ? Ss Oct20 0:00 /usr/sbin/sshd -D |
您可以在ps命令中进行过滤,例如
1 | ps u -C gnome-terminal |
(或通过/ proc搜索并查找等)
在搜索模式中使用方括号将字符括起来会排除
1 2 3 4 5 6 7 8 9 10 | $ 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 |
另一种选择:
1 | ps -fC terminal |
这里的选项:
1 2 3 4 5 6 7 8 9 10 11 | -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. |
免责声明:我是这个工具的作者,但是...
我会用px:
1 2 3 4 5 6 7 | ~ $ 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,易于安装:
1 | curl -Ls https://github.com/walles/px/raw/python/install.sh | bash |
根据最终用例,您通常希望改用Awk。
1 | ps aux | awk '/[t]erminal/' |
当您遇到类似问题时,尤其如此
1 | ps aux | grep '[t]erminal' | awk '{print $1}' # useless use of grep! |
显然,正则表达式可以轻松地分解到Awk脚本中:
1 | ps aux | awk '/[t]erminal/ { print $1 }' |
但实际上,不要自己重塑。
另一个选择是编辑
1 2 3 4 5 | function mygrep { grep -v grep | grep --color=auto $1 } alias grep='mygrep' |
如果您正在使用bash,则此方法有效;如果您使用的是其他外壳YMMV。