How do I show usernames in top?

1

I'd like to do something like top -o cpu but with usernames for each process shown in each row. How can I do this? Thanks!

p.s. I am on Mac OS. I am not sure if the top command is different from other non-BSD.

ceiling cat

Posted 2012-09-08T06:41:50.993

Reputation: 3 557

The linux man-page for top here indicates that the output of the command contains UID, perhaps you can use this information to obtain the username as indicated in this forum post. Not sure how easy its would be to automate the entire thing...

– Bhargav Bhat – 2012-09-08T07:42:56.137

Answers

2

Note This solution is Mac OS specific. The Linux version is significantly different.

top does not show the username by default just the UID.

You can choose which columns to show with the -stats option.

For example

top -stats pid,user,cpu,command

will show the process ID, the user name, the CPU usage and the command

Processes: 167 total, 2 running, 4 stuck, 161 sleeping, 828 threads                                                                                                                                       09:55:43
Load Avg: 0.99, 1.31, 1.72  CPU usage: 7.6% user, 12.7% sys, 80.86% idle    SharedLibs: 11M resident, 10M data, 0B linkedit. MemRegions: 46953 total, 2034M resident, 55M private, 708M shared.
PhysMem: 1166M wired, 2962M active, 849M inactive, 4977M used, 3213M free. VM: 402G vsize, 1054M framework vsize, 13229803(2) pageins, 816120(0) pageouts.
Networks: packets: 909991873/986G in, 888624532/347G out. Disks: 35379646/317G read, 10360185/448G written.

PID                                              USER                                                     %CPU                                           COMMAND
89849                                            root                                                     0.0                                            installd
87409                                            corti                                                    0.0                                            cookied
75165                                            root                                                     8.4                                            top
75163                                            corti                                                    0.0                                            mdworker

You can see the list with man top:

          pid    Process ID (default).

          command
                 Command name.

          cpu    CPU usage.

          csw    Number of context switches.

          time   Execution time.

          threads
                 alias: th
                 Number of threads (total/running).

          ports  alias: prt
                 Number of Mach ports.

          mregion
                 alias: mreg, reg
                 Number of memory regions.

          rprvt  Resident private address space size.

          rshrd  Resident shared address space size.

          rsize  Resident memory size.

          vsize  Total memory size.

          vprvt  Private address space size.

          kprvt  Private kernel memory size.

          kshrd  Shared kernel memory size.

          pgrp   Process group id.

          ppid   Parent process id.

          state  alias: pstate
                 Process state.

          uid    User ID.

          wq     alias: #wq, workqueue
                 The workqueue total/running.

          faults alias: fault
                 The number of page faults.

          cow    alias: cow_faults
                 The copy-on-write faults.

          user   alias: username Username.

          msgsent
                 Total number of mach messages sent.

          msgrecv
                 Total number of mach messages received.

          sysbsd Total BSD syscalls.

          sysmach
                 Total Mach syscalls.

          pageins
                 Total pageins.

Matteo

Posted 2012-09-08T06:41:50.993

Reputation: 6 553