首页 > 解决方案 > 如何在 ps -eo pid,ppid,%mem,%cpu,cmd --sort=-%mem 的 ps 命令中获取短进程名称(cmd 参数)头

问题描述

执行命令

ps -eo pid,ppid,%mem,%cpu,cmd --sort=-%mem | head

我得到:

   PID PPID %MEM %CPU CMD
   1555 1 4.2 0.2 /usr/sbin/mysqld
   2989 2735 3.9 4.0 /usr/bin/gnome-shell
   4785 2735 3.8 3.3 /opt/google/chrome/chrome
   5053 4803 3.6 1.4 /opt/google/chrome/chrome --type=renderer --field-trial-handle=14183456324498441308,5334468187998544589,131072 --lang=en-GB --enable-crash-reporter=e482c1e6-5c57-40e8 -bb27-910eab6576d7, --extension-process --origin-trial-disabled-features=SecurePaymentConfirmation --num-raster-threads=4 --enable-main-frame-before-activation --renderer-client-id=12 --no-v8-untrusted-code-mitigations --shared-files=v8_context_snapshot_data:100
   5054 4803 3.5 0.6 /opt/google/chrome/chrome --type=renderer --field-trial-handle=14183456324498441308,5334468187998544589,131072 --lang=en-GB --enable-crash-reporter=e482c1e6-5c57-40e8 -bb27-910eab6576d7, --extension-process --origin-trial-disabled-features=SecurePaymentConfirmation --num-raster-threads=4 --enable-main-frame-before-activation --renderer-client-id=13 --no-v8-untrusted-code-mitigations --shared-files=v8_context_snapshot_data:100
  

在上面的输出中,我只想查看chromeoropt/google/chrome/chrome而不是完整的名称。

标签: linuxps

解决方案


根据ps 手册页的STANDARD FORMAT SPECIFIERS部分,关键字是关键字的别名,它将显示命令及其所有参数。请改用关键字,它将仅显示命令名称。cmdargscomm

ps -eo pid,ppid,%mem,%cpu,comm --sort=-%mem | head

comm如果命令名称未完全显示,您也可以使用宽度说明符来指定显示的确切字符数(如果是最后一列,则不会有任何区别)

ps -eo pid,ppid,%mem,%cpu,comm:20 --sort=-%mem | head


推荐阅读