Score:0

ps aux | grep command doesn't display categories

ke flag

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
Score:0
hr flag

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
Onizuka avatar
ke flag
That is good to know. Thanks! Maybe you can explain something related below.
Onizuka avatar
ke flag
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.
hr flag
@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)
Onizuka avatar
ke flag
Oops, didn't even catch that they were backticks. But this is a useful link, thanks again!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.