Find out the resource(cpu, memory) usages of users

In some high load situations, we need to find out the resource consumption of each user in the server to figure out the exact issue. There are a lot of options/commands to find the same. Normally, we are using the “ps” and “top” command for performing this. Separating or sorting Unix user with their own resource usage is quit hard with a Linux beginner.  The command PS has a lot of switches for sorting processes in different manner. We can simply sort the current executing processes with its CPU and Memory conception ratio. Also, here is a simple link for monitoring your server load “Load monitoring in Linux servers“

“ps command” and “top command” have a lot of options, here I am explaining some useful command combinations to find the resource(cpu, memory…) usages of users in the server.

It’s simply a static output of current resource usage in the server. You can use the grep command to separate users.

To list top 10 CPU usage processes with user

# ps -e -o pcpu,pid,user,args|sort -k1 -nr|head -10

Output

[root@Coimbatorewebhosting~]# ps -e -o pcpu,pid,user,args|sort -k1 -nr|head -10
%CPU   PID    USER     COMMAND
 0.0     9    root     [rcu_sched]
 0.0  9202    vnstat   /usr/sbin/vnstatd -n
 0.0     8    root     [rcu_bh]
 0.0  8907    root     [kworker/u4:2]
 0.0     7    root     [migration/0]
 0.0   749    postfix  qmgr -l -t unix -u
 0.0   746    root     /usr/libexec/postfix/master -w
 0.0    68    root     [deferwq]
 0.0     5    root     [kworker/0:0H]
[root@Coimbatorewebhosting~]#

Find out top 10 CPU consuming process

# ps -auxf|sort -nr -k3|head -10

Output

[root@Coimbatorewebhosting~]# ps -auxf|sort -nr -k3|head -10
vnstat    9202  0.0  0.0   7444   848 ?        Ss   Feb19   0:48 /usr/sbin/vnstatd -n
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         9  0.0  0.0      0     0 ?        S    Feb19   3:56  \_ [rcu_sched]
root      8907  0.0  0.0      0     0 ?        S    Feb19   0:00  \_ [kworker/u4:2]
root         8  0.0  0.0      0     0 ?        S    Feb19   0:00  \_ [rcu_bh]
root       746  0.0  0.2  89036  2172 ?        Ss   Feb19   0:04 /usr/libexec/postfix/master -w
root         7  0.0  0.0      0     0 ?        S    Feb19   0:00  \_ [migration/0]
root        68  0.0  0.0      0     0 ?        S<   Feb19   0:00  \_ [deferwq]
root         5  0.0  0.0      0     0 ?        S<   Feb19   0:00  \_ [kworker/0:0H]
root       494  0.0  0.0 110036   844 tty1     Ss+  Feb19   0:00 /sbin/agetty --noclear tty1 linux
[root@Coimbatorewebhosting~]#

To list top 10 Memory consuming processes with user

# ps -e -o pmem,pid,user,args|sort -k1 -nr|head -10

Output

[root@Coimbatorewebhosting~]# ps -e -o pmem,pid,user,args|sort -k1 -nr|head -10
 1.6   450 root     /usr/bin/python -Es /usr/sbin/tuned -l -P
 1.3 19168 root     /usr/sbin/httpd -DFOREGROUND
 0.8   457 polkitd  /usr/lib/polkit-1/polkitd --no-debug
 0.7 23926 apache   /usr/sbin/httpd -DFOREGROUND
 0.7 23925 apache   /usr/sbin/httpd -DFOREGROUND
 0.7 23924 apache   /usr/sbin/httpd -DFOREGROUND
 0.7 23424 apache   /usr/sbin/httpd -DFOREGROUND
 0.7 23388 apache   /usr/sbin/httpd -DFOREGROUND
 0.7 23387 apache   /usr/sbin/httpd -DFOREGROUND
 0.7 23386 apache   /usr/sbin/httpd -DFOREGROUND
[root@Coimbatorewebhosting~]#

Find out the top 10 memory consuming process

# ps -auxf|sort -nr -k4|head -10

Output

[root@Coimbatorewebhosting~]# ps -auxf|sort -nr -k4|head -10
root       450  0.0  1.6 553152 16436 ?        Ssl  Feb19   2:12 /usr/bin/python -Es /usr/sbin/tuned -l -P
root     19168  0.0  1.3 330364 13248 ?        Ss   Feb19   1:01 /usr/sbin/httpd -DFOREGROUND
polkitd    457  0.0  0.8 528308  8844 ?        Ssl  Feb19   0:00 /usr/lib/polkit-1/polkitd --no-debug
apache   23926  0.0  0.7 330364  7784 ?        S    Feb26   0:00  \_ /usr/sbin/httpd -DFOREGROUND
apache   23925  0.0  0.7 330364  7784 ?        S    Feb26   0:00  \_ /usr/sbin/httpd -DFOREGROUND
apache   23924  0.0  0.7 330364  7788 ?        S    Feb26   0:00  \_ /usr/sbin/httpd -DFOREGROUND
apache   23424  0.0  0.7 330364  7772 ?        S    Feb25   0:00  \_ /usr/sbin/httpd -DFOREGROUND
apache   23388  0.0  0.7 330364  7788 ?        S    Feb25   0:00  \_ /usr/sbin/httpd -DFOREGROUND
apache   23387  0.0  0.7 330364  7788 ?        S    Feb25   0:00  \_ /usr/sbin/httpd -DFOREGROUND
apache   23386  0.0  0.7 330364  7768 ?        S    Feb25   0:00  \_ /usr/sbin/httpd -DFOREGROUND
[root@Coimbatorewebhosting~]#

Find out every process running under a user

# ps -U user-name -u user-name u

Or

# ps -e -o pid,user,args|grep $username

Output

[root@Coimbatorewebhosting~]# ps -e -o pid,user,args|grep root
    1 root     /usr/lib/systemd/systemd --switched-root --system --deserialize 21
    2 root     [kthreadd]
    3 root     [ksoftirqd/0]
    5 root     [kworker/0:0H]
    7 root     [migration/0]
    8 root     [rcu_bh]
    9 root     [rcu_sched]
   10 root     [watchdog/0]
   11 root     [watchdog/1]
   12 root     [migration/1]
   13 root     [ksoftirqd/1]
   15 root     [kworker/1:0H]
   17 root     [kdevtmpfs]
   18 root     [netns]

To get a dynamic result you must use the ‘top‘ command instead of ‘ps’ or use the ‘watch‘ command along with the ‘ps’.

To show the process usage of a user with ‘top’

# top -u $username

Output

top - 11:47:03 up 7 days, 23:53,  2 users,  load average: 0.00, 0.01, 0.05
Tasks:  76 total,   2 running,  74 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.2 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1012436 total,   370668 free,    68052 used,   573716 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   906684 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
23384 apache    20   0  330364   7768   1936 S   0.0  0.8   0:00.00 httpd
23385 apache    20   0  330364   7788   1932 S   0.0  0.8   0:00.00 httpd
23386 apache    20   0  330364   7768   1936 S   0.0  0.8   0:00.00 httpd
23387 apache    20   0  330364   7788   1932 S   0.0  0.8   0:00.00 httpd
23388 apache    20   0  330364   7788   1936 S   0.0  0.8   0:00.00 httpd
23424 apache    20   0  330364   7772   1936 S   0.0  0.8   0:00.00 httpd
23924 apache    20   0  330364   7788   1936 S   0.0  0.8   0:00.00 httpd
23925 apache    20   0  330364   7784   1932 S   0.0  0.8   0:00.00 httpd
23926 apache    20   0  330364   7784   1932 S   0.0  0.8   0:00.00 httpd

You can refer this “How to show process usage for single user with TOP command” for more details

To list top 10 CPU usage processes with user

# watch "ps -e -o pcpu,pid,user,args|sort -k1 -nr|head -10"

Output

0.4 24497 root     watch ps -e -o pcpu,pid,user,args|sort -k1 -nr|head -10
%CPU   PID USER     COMMAND
 0.0     9 root     [rcu_sched]
 0.0  9202 vnstat   /usr/sbin/vnstatd -n
 0.0     8 root     [rcu_bh]
 0.0  8907 root     [kworker/u4:2]
 0.0     7 root     [migration/0]
 0.0   749 postfix  qmgr -l -t unix -u
 0.0   746 root     /usr/libexec/postfix/master -w
 0.0    68 root     [deferwq]

To list top 10 Memory consuming processes with user

# watch "ps -e -o pmem,pid,user,args|sort -k1 -nr|head -10"

instead you can use the grep command to separate users.