IT

더 우아한“ps aux |

lottoking 2020. 5. 29. 08:20
반응형

더 우아한“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마지막 항목을 제거 하는 사용 하지만 우아 하지는 않습니다. :)

이 문제를 해결하기 위해보다 우아한 해킹이 있습니까? (모든 명령을 별도의 스크립트로 래핑하는 것 외에도 나쁘지 않습니다)


일반적인 기술은 다음과 같습니다.

ps aux | egrep '[t]erminal'

이 포함 된 행 일치 terminal하는 egrep '[t]erminal'하지 않습니다! 또한 많은 유닉스 맛에서 작동합니다 .


pgrep을 사용하십시오 . 더 안정적입니다.


이 답변은 이전에 구축 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

예를 들어 ps 명령에서 필터링 할 수 있습니다.

ps u -C gnome-terminal

(또는 찾기 등으로 / proc를 통해 검색 )


하나 더 대안 :

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.

검색 패턴에서 대괄호를 사용하여 문자를 둘러싸 grep면 일치하는 정규식이 포함되지 않으므로 프로세스가 제외됩니다 .

$ 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

Except for finding processes with a sensible command line interface it also does a lot of other useful things, more details on the project page.

Works on Linux and OS X, easily installed:

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

Depending on the ultimate use case, you often want to prefer Awk instead.

ps aux | awk '/[t]erminal/'

This is particularly true when you have something like

ps aux | grep '[t]erminal' | awk '{print $1}'  # useless use of grep!

where obviously the regex can be factored into the Awk script trivially:

ps aux | awk '/[t]erminal/ { print $1 }'

But really, don't reinvent this yourself. pgrep and friends have been around for a long time and handle this entire problem space much better than most ad hoc reimplementations.


Another option is to edit your .bash_profile (or other file that you keep bash aliases in) to create a function that greps 'grep' out of the results.

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

alias grep='mygrep'

The grep -v grep has to be first otherwise your --color=auto won't work for some reason.

This works if you're using bash; if you're using a different shell YMMV.

참고URL : https://stackoverflow.com/questions/9375711/more-elegant-ps-aux-grep-v-grep

반응형