15

I would like to know how I could get the number of processes for each user that is currently logged in.

9 Answers 9

13

You could try some variation of this:

ps haux Ou | cut '-d ' -f1 | uniq -c

It gives you the number of processes for each users (being logged in or not). Now you could filter those results using the output of the w command or another way of determining who is logged in.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice! Could add a grep ' pts\| tty' before the cut, would cut out any process not tied to a terminal.
6

Give this a try:

ps -u "$(echo $(w -h | cut -d ' ' -f1 | sort -u))" o user= | sort | uniq -c | sort -rn

In order to properly handle usernames that may be longer than eight characters, use users instead of w. The latter truncates usernames.

ps -u "$(echo $(printf '%s\n' $(users) | sort -u))" o user= | sort | uniq -c | sort -rn

2 Comments

Unfortunately w -h truncates user names to 8 characters, it also only lists the logged on users so you don't get a view of all users, just logon users.
@OliverDungey: The OP asked for users that are currently logged in. If you want processes for all users regardless of whether they're logged in then you don't need w - you can just use ps (which can be made to show the long usernames in full). The users command shows logged in users and appears to not truncate the names. My answer could be adapted to use it. I'll update my answer.
3
ps -u aboelnour | awk 'END {print NR}' 

will show number of process which user aboelnour running it

Comments

3

If you are ever concerned about nearing the user process limit shown by ulimit -a, the you want to get ALL the processes (including LWPs). In such a case you should use:

ps h -Led -o user | sort | uniq -c | sort -n

On one system doing this:

ps haux Ou | cut '-d ' -f1 | uniq -c

yields:

# ps haux Ou | cut '-d ' -f1 | uniq -c
 30 user1
  1 dbus
  3 user2
  1 ntp
  1 nut
  1 polkitd
  2 postfix
124 root
  2 serv-bu+

where doing the former yields the true process count:

# ps h -Led -o user | sort | uniq -c | sort -n
  1 ntp
  1 nut
  2 dbus
  2 postfix
  2 serv-builder
  3 user2
  6 polkitd
141 root
444 user1

Comments

3

Just try:

lslogins -o USER,PROC

1 Comment

Nice. Was introduced in 2014, 4 years after the question, and until today I hadn't stumbled upon it =}
1

If you just want a count of processes you can use procfs directly like this: (requires linux 2.2 or greater)

you can use wc:

number_of_processes=`echo /proc/[0-9]* | wc -w`

or do it in pure bash (no external commands) like this

procs=( /proc/[0-9]* ) 
number_of_proccesses=${#procs[*]}

If you only want the current userid

procs=( /proc/[0-9]*/fd/. ) 
number_of_proccesses=${#procs[*]}

Comments

0
userlist=$(w|awk 'BEGIN{ORS=","}NR>2{print $1}'|sed 's/,$//' )
ps -u "$userlist"

1 Comment

I have used this in combination with Aboelnour's answer but it gives me the number of processes without the corresponding user of those processes.Any idea on how I could fix this?
0

Following links contain useful ps commands options including your requirements:

Comments

0

Here is my solution, for Linux:

$ find /proc –user $USER -maxdepth 1 -name '[0-9]*' | wc –l

This solution will not fail when the number of processes is larger than the command line limit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.