16

How can i create a script to count process run by each user in ps aux. I used this

ps aux  | awk '{print $1}' | grep root | wc -l

but it lists count of root user only . I want to list number of process for each user. I need something like this:

root    20
jamshi  15 
1
  • .[root@server1/]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME root 1 0.0 0.0 19236 180 ? SNs Aug15 0:03 root 2 0.0 0.0 0 0 ? SN Aug15 0:00 root 3 0.0 0.0 0 0 ? SN Aug15 0:00 named 31745 0.0 0.6 697276 3236 ? Ssl Aug17 0:14 I need result like below = root 3 named 1
    – Jamsheer P
    Commented Aug 29, 2015 at 12:15

2 Answers 2

27

ps -fo user | sort | uniq -c is worth a try.

The command ps -eo user=|sort|uniq -c will list process counts by user.

ps -eo user=|sort|uniq -c
      2 avahi
      1 kernoops
      1 messagebus
      1 nobody
    231 root
      1 statd
      5 steve
      1 syslog

If flipping the column order to read is required, pipe it through awk '{ print $2 " " $1 }'

1
  • Thanks for your reply .[root@server1/]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME root 1 0.0 0.0 19236 180 ? SNs Aug15 0:03 root 2 0.0 0.0 0 0 ? SN Aug15 0:00 root 3 0.0 0.0 0 0 ? SN Aug15 0:00 named 31745 0.0 0.6 697276 3236 ? Ssl Aug17 0:14 result of your command ===== [root@server1 /]# ps -fo user | sort | uniq -c 4 root 1 USER I need result like below = root 3 named 1
    – Jamsheer P
    Commented Aug 29, 2015 at 12:12
3

You can try

ps aux |
awk 'NR>1{tot[$1]++;}
     END{for(id in tot)printf "%s %s\n",id,tot[id]}'
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .