linux - 如何在 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
在上面的输出中,我只想查看chrome
oropt/google/chrome/chrome
而不是完整的名称。
解决方案
根据ps 手册页的STANDARD FORMAT SPECIFIERS部分,关键字是关键字的别名,它将显示命令及其所有参数。请改用关键字,它将仅显示命令名称。cmd
args
comm
ps -eo pid,ppid,%mem,%cpu,comm --sort=-%mem | head
comm
如果命令名称未完全显示,您也可以使用宽度说明符来指定显示的确切字符数(如果是最后一列,则不会有任何区别)
ps -eo pid,ppid,%mem,%cpu,comm:20 --sort=-%mem | head
推荐阅读
- android - 如何解决无法恢复密钥,android studio 4.1.3
- arrays - 访问第一列之前列的矩阵中的值?
- google-apps-script - 使用 .gs 获取谷歌表中的最新值或范围内的最后一个值
- ios - 计时器运行并更改标签文本时重新应用约束
- spring-boot - 如何从 Springboot 读取 Windows 共享文件夹中的文件?
- reactjs - 将 React js 与现有的 laravel 项目合并
- python - Python Lambda 函数成功,但未显示任何行已更新
- c# - C# 架构指南 - 批处理作业
- arrays - 我如何初始化@Binding SwiftUi
- html - 如何将图像数据从 html 表单获取到烧瓶?