-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminor2.sh
More file actions
56 lines (43 loc) · 1.8 KB
/
minor2.sh
File metadata and controls
56 lines (43 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#! /bin/bash
#Author: Jeremy Kracy
#EUID: jek0138
#Section: CSCE 3600.002
#Description: If no command line arguments are passed in, prints out users and which
# how many processes each is running, along with total number of users
# and processes running
# Accepts user IDs as command line inputs, if IDs are provided it will
# only perform the above processes with the provided users
leaving() #Function for if a SIGINT is entered
{
echo " (SIGINT) recieved"
read -p "Terminate Program? (Y/N) " choice #Prompt user to exit
case $choice in
Y|y) echo "Terminating program" #Exit
exit 1;;
*) echo "Continuing Execution";; #continue
esac
}
trap leaving SIGINT #Go to leaving function if ^C is entered
while true #Loop until stop is requested
do
numUser=0 #set number of users and number of processes to 0
numProc=0 #at the start of each loop
date #print out date
printf "%-20s%-10s\n" "User ID" "Count" #print out header for output
if [ $# -ne 0 ] #Check if command line arguments were entered
then
numUser=$# #Set num users to number of arguments entered
for i in $@ #loop through entered users
do
let numProc=$numProc+`ps -u $i|wc -l` #add up processes for each entered user
printf "%-20s%-10s\n" $i `ps -u $i|wc -l` #Print out each user and its number of processes
done
else
numUser=`ps -eo user|sort|uniq|wc -l` #Set num users to the amount of users on
numProc=`ps aux|wc -l` #Set num processes to mount of processes running
ps -eo user=|sort|uniq -c | gawk '{ printf "%-20s%-10s\n", $2, $1 };'
fi
echo "$numUser USERS, TOTAL PROCESSES $numProc" #Final output
echo "" #new line
sleep 5 #sleep for 5 seconds
done