Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The number of processes a user is running using bash

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

like image 215
Vidi Avatar asked Oct 06 '10 16:10

Vidi


People also ask

How do I see what processes are running in bash?

Bash commands to check running process: pgrep command – Looks through the currently running bash processes on Linux and lists the process IDs (PID) on screen. pidof command – Find the process ID of a running program on Linux or Unix-like system.

How can I tell how many processes are running?

You can list running processes using the ps command (ps means process status). The ps command displays your currently running processes in real-time.

How would you count the number of processes using ps and wc?

You can use ps (will show snapshot of processes) with wc (will count number of words, wc -l option will count lines i.e. newline characters). Which is very easy and simple to remember. This simple command will print number of processes running on current server.


2 Answers

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.

like image 185
Helmut Grohne Avatar answered Oct 26 '22 14:10

Helmut Grohne


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
like image 41
Dennis Williamson Avatar answered Oct 26 '22 13:10

Dennis Williamson