Advertisement
dzocesrce

[OS] User Processes and No. of their Childs

Jan 20th, 2024 (edited)
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.03 KB | None | 0 0
  1. #!/bin/bash
  2. #check if the number of arguments is right
  3. if [ $# -ne 1 ]
  4. then
  5.     echo "USAGE: `basename $0` korisnik"
  6.     exit 1
  7. fi
  8. #check if the out.txt file already exists
  9. if [ -f out.txt ]
  10. then
  11.     rm out.txt
  12. fi
  13. #create the out.txt file
  14. touch out.txt
  15. echo "PID|Number of kids" >> out.txt
  16. #get all process IDz of all the processes the argument user has started
  17. pids=`ps -es | grep ^$1 | awk 'print $2;'`
  18. #go thru all of em
  19. for pid in $pids
  20. do
  21.     count=0
  22.     #get the parent process IDz of the processes the user had started
  23.     ppids=`ps -es | grep ^$1 | awk 'print $3;'`
  24.     #if the process the user started is a parent of some other process then count++
  25.     if [ $pid -eq $ppid ]
  26.     then
  27.         count=`expr $count + 1`
  28.     fi
  29.     #put in a line of text in out.txt for every cycle iteration
  30.     #that consists of the PID of each process the user had started
  31.     #and tha number of his child processes separted with tha | symbol
  32.     echo "${pid}|${count}" >> out.txt
  33. done
  34. #print on screen tha output in need
  35. cat out.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement