0

I am trying to get the top five processes of a specific user in a bash:

ps -Ao user,uid,comm,pid,pcpu,tty --sort=-pcpu | head -n 6 | grep <username> 

However, this selects the top 5 processes across the system and then filters out the processes that belong to the specified username. I basically want to flip the logic. Get all processes of a specific user and then filter the top 5.

2
  • So, exchange the head and the grep? Commented Aug 31, 2019 at 16:21
  • Thanks @Kusalananda. Works like a charm! Commented Aug 31, 2019 at 16:24

2 Answers 2

1

In your pipeline, the head command would deliver the first six lines of output from ps to grep, no matter what username was mentioned in those lines. Swapping the head and grep calls around would mean that the head command would only ever see lines from ps containing the wanted username. (You also likely want to use head -n 5).

0

ps -u username

It will list user process

You can pipeline output accordingly to get 5 process

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.