How to show CPU time for processes via top without ‘root’ procs

awkgreppssorttop

Okay, So I've been attempting this for about three hours now without success.

How do you search/display/use the top command (or ps command if it works…) to output a list of all procs, sorted by CPU time Excluding procs owned by 'root'.

My attempts so far:

top -b -S -n 1 | grep -v root
top -b -S -n 1 | egrep -ve root

ps -eo pid,user,args,etime,time,%cpu --sort etime | egrep -v root

That and various attempts at running top in batch mode, outputting to a file and attempting to awk/grep/sort through it and sort it properly by the amount of CPU time (mostly unable to find the right column / manage to sort the right column in any seemingly useful way).

Forgive me if that seems a bit of a muddle; tl;dr:

I just want some way to easily read top without root procs and sorted by CPU time.

Best Answer

Your ps command should work if you sort it properly. From man ps:

   --sort spec
          Specify sorting order.  Sorting syntax is
          [+|-]key[,[+|-]key[,...]].  Choose a multi-letter key from the
          STANDARD FORMAT SPECIFIERS section.  The "+" is optional since
          default direction is increasing numerical or lexicographic
          order.  Identical to k.  For example: ps jax --sort=uid,-ppid,
          +pid

I'm not sure which time you want to sort by but here are the relevant choices:

STANDARD FORMAT SPECIFIERS
   bsdtime     TIME      accumulated cpu time, user + system.  The display
                         format is usually "MMM:SS", but can be shifted to
                         the right if the process used more than 999
                         minutes of cpu time.

   cputime     TIME      cumulative CPU time, "[DD-]hh:mm:ss" format.
                         (alias time).
   etime       ELAPSED   elapsed time since the process was started, in
                         the form [[DD-]hh:]mm:ss.

   etimes      ELAPSED   elapsed time since the process was started, in
                         seconds.

I think from your question that you want cputime. If so, this should give you your desired output:

ps -eo pid,user,args,etime,time,%cpu --sort cputime | grep -v root
Related Question