亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

波斯汪 2019-12-10 13:08:22
當我檢查過程列表并“ grep”出對我來說很有趣的過程時,其grep本身也包含在結果中。例如,列出終端:$ ps aux  | grep terminaluser  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用來擺脫最后一個條目...但是它并不優雅 :)您是否有更優雅的技巧來解決此問題(將所有命令包裝到單獨的腳本中,這也不錯)
查看完整描述

3 回答

?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

通常的技術是這樣的:


ps aux | egrep '[t]erminal'

這將匹配包含的行terminal,但egrep '[t]erminal'不匹配!它也可以在許多 Unix版本上使用。



查看完整回答
反對 回復 2019-12-11
?
慕神8447489

TA貢獻1780條經驗 獲得超1個贊

它還建立在另一個答案結合使用的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



查看完整回答
反對 回復 2019-12-11
?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

另一種選擇:


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.



查看完整回答
反對 回復 2019-12-11
  • 3 回答
  • 0 關注
  • 740 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號