프로세스 목록을 확인하고 나에게 흥미로운 프로세스를 ‘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
현명한 명령 행 인터페이스로 프로세스를 찾는 것 외에 다른 유용한 것들도 많이 있습니다. 자세한 내용 은 프로젝트 페이지를 참조하십시오 .
Linux 및 OS X에서 작동하며 쉽게 설치할 수 있습니다.
curl -Ls https://github.com/walles/px/raw/python/install.sh | bash
