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

当我检查过程列表并“ grep”出对我来说很有趣的过程时,其grep本身也包含在结果中。例如,列出终端:


$ 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

通常我ps aux | grep something | grep -v grep用来摆脱最后一个条目...但是它并不优雅 :)


您是否有更优雅的技巧来解决此问题(将所有命令包装到单独的脚本中,这也不错)



波斯汪
浏览 623回答 3
3回答

慕村9548890

通常的技术是这样的:ps aux | egrep '[t]erminal'这将匹配包含的行terminal,但egrep '[t]erminal'不匹配!它也可以在许多 Unix版本上使用。

慕神8447489

它还建立在另一个答案结合使用的ps与pgrep。以下是一些相关的培训示例:$ pgrep -lf sshd1902 sshd$ pgrep -f sshd1902$ ps up $(pgrep -f sshd)USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMANDroot      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 sshdUSER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMANDroot      1902  0.0  0.1  82560  3580 ?        Ss   Oct20   0:00 /usr/sbin/sshd -D用比较ps有grep。不会打印出有用的标题行:$  ps aux | grep [s]shdroot      1902  0.0  0.1  82560  3580 ?        Ss   Oct20   0:00 /usr/sbin/sshd -D

哔哔one

另一种选择: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.
打开App,查看更多内容
随时随地看视频慕课网APP