0

On AIX, for the top 10 cpu processes, I use

ps aux | head -1; ps aux | sort -rn +2 | head -10

for the top 10 mem processes

ps aux | head -1; ps aux | sort -rn +3 | head -10

How can I sort the output by stime?

stime is column 9 but

ps aux | head -1; ps aux | sort -rn +8 | head -10

Gives me this result -- not sort by stime

user 14614674  0,0  0,0  168  176  pts/0 T    14:23:23  0:00 cat 
user 13041722  0,0  0,0  168  176  pts/0 T    14:25:59  0:00 cat 
user 12517628  0,0  0,0  168  176  pts/0 T    14:22:26  0:00 cat 
user  9043992  0,0  0,0  168  176  pts/0 T    14:23:13  0:00 cat 
user 14745724  0,0  0,0  168  176  pts/0 T    13:47:11  0:00 cat 
1

+8 (to sort on the part of the line starting with the 9th field) or +8 -9 (to sort on the 9th field) is the deprecated sort key specification syntax. I'd recommend switching to the modern one: -k 9 and -k 9,9.

In your case here, with -n the sort key is parsed as a number. That is, the portion of the line that starts at the 9th field and ends at the end of the line is converted to a number, and it's that number that is used for sorting.

That means that only the hour part will be considered.

For the minutes and seconds to be considered, you could do:

ps aux | sort -r -k9n -k9,9

Then, for ties on the comparison of hours, do a lexical comparison. If the time for morning hours are written as 02:12 and not 2:12, you can just do sort -rk9, that is do a lexical comparison.

Note that it will probably not work if there are processes that were started yesterday or before.

  • The question should have said stime is column 9, not 8, counting from 1. – meuh Mar 3 '16 at 14:58
  • @meuh. D'oh. Well spotted. – Stéphane Chazelas Mar 3 '16 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.