0

When I pipe to grep after a ps aux command it isn't showing the categories at the top of the list (USER, PID, %CPU, %MEM, etc) Is there something I can do about this?

ps aux --sort -rss | grep $USER | head -n 4
user01      1610  0.0  0.3  17968 10156 ?        Ss   Jan19   0:01 /lib/systemd/systemd --user

user01      1611  0.0  0.0 104400  2108 ?        S    Jan19   0:00 (sd-pam)

user01      1617  0.0  0.1  48216  4812 ?        S<sl Jan19   0:00 /usr/bin/pipewire

user01      1618  0.0  0.1  32108  4256 ?        Ssl  Jan19   0:00 /usr/bin/pipewire-media-session

I am expecting to see the following at the very top:

USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
Onizuka
  • 3
  • 2

1 Answers1

0

Aside from the issue of omitting the header line, piping ps aux output through grep $USER is an unreliable way to select only processes owned by your user, it may:

  • not match at all, since the USER column may be truncated

  • match elsewhere in the output line (such as a command name or argument)

You could do something hacky like ps aux --sort -rss | grep -E "^(${USER:0:7}|USER)" | head -n 5, however if you want only the current user's processes you can simply omit the a option from your ps command:

$ ps ux --sort -rss | head -n 5
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
steeldr+    1279  0.0  4.5 416748 44804 ?        Rl   03:27   0:12 /usr/bin/qterminal
steeldr+    1102  0.0  4.2 907696 42420 ?        Sl   03:27   0:11 /usr/bin/lxqt-panel
steeldr+    1095  0.0  3.9 853820 39056 ?        Sl   03:27   0:00 /usr/bin/pcmanfm-qt --desktop --profile=lxqt
steeldr+    1101  0.0  2.2 405672 22252 ?        Sl   03:27   0:00 /usr/bin/lxqt-notificationd
steeldriver
  • 131,985
  • 21
  • 239
  • 326
  • That is good to know. Thanks! Maybe you can explain something related below. – Onizuka Jan 25 '23 at 18:17
  • So I was using the $USER as a workaround to the problem example not working correctly. The example they gave: `ps aux --sort -rss | grep -i 'whoami' | head -n 4` but the output only ever shows one line no matter what number is specified in the head command. I'm supposed to figure out a different way to write it, but am wondering if the `'whoami'` could actually be made to work. – Onizuka Jan 25 '23 at 18:27
  • @Onizuka the example should use backticks or better the more modern syntax `$(whoami)` not single quotes, to execute `whomai` as a command substitution rather than a string literal. See [Differences between doublequotes " ", singlequotes ' ' and backticks ` ` on commandline?](https://askubuntu.com/questions/20034/differences-between-doublequotes-singlequotes-and-backticks-on-comm) – steeldriver Jan 25 '23 at 18:40
  • Oops, didn't even catch that they were backticks. But this is a useful link, thanks again! – Onizuka Jan 25 '23 at 19:12