GNU/Linux >> Linux の 問題 >  >> Linux

よりエレガントな ps aux | grep -v grep

pgrep を使用します。より信頼性が高くなります。


通常のテクニックは次のとおりです:

ps aux | egrep '[t]erminal'

これは terminal を含む行に一致します 、これ egrep '[t]erminal' ではない! 多くのでも機能します Unix のフレーバー。


この回答は、以前の 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 [email protected]) 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

Linux
  1. Recursive Grep Vs Find / -type F -exec Grep {};どちらがより効率的/高速ですか?

  2. Grep -v予期しない動作?

  3. スクリプトが実行されていることを確認しますか?

  1. Cut / Grep And Df -h?

  2. Grep And Tail -f?

  3. 再帰的にgrepするにはどうすればよいですか?

  1. カットを使用して区切り文字にさらにスペースを指定する方法は?

  2. ファイル内で \n を grep する方法

  3. ps aux の出力を見る | tmux の grep blah は機能しませんか?