User processes

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    I have to write a program which can tell me how many processes is running by some user, from the /etc/passwd file
    it should look like this:
    root 50
    me 55
    other 2

  2. Relevant commands, code, scripts, algorithms:
    cut, pgrep

  3. The attempts at a solution (include all code and scripts):
    i have done this so far but it just gives me the sum of all processes running on all users:

user=$(cut -d: -f1 /etc/passwd)
process=$(( pgrep -cu $user))
echo -e "$user"

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    University of Ljubljana, Ljubljana, Slovenia, Janez Novak, ID63709

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Some tips:

use whie loop to read the /etc/passwd file and get the users one by one and then get the process count of that user and print it.

cheers,
Devaraj Takhellambam

Based on your code:

#!/bin/bash

USERS=$( cut -d: -f1 /etc/passwd )
for NAME in $USERS
do
  PROCS=$( pgrep -cu $NAME )
  echo "User: $NAME > Procs: $PROCS"
done

exit 0
#finis
1 Like

Thaaaanks it works very good, just like intended :wink: