#!/bin/sh
# get forked processes for every user part of the efflab
# Written by hip0
for L in $(ps ax|egrep " <.*>$"|awk '{ print $5 }'|sed -e 's#<##g' -e 's#>##g'); do 
((F++));
USERS[$F]="$L";

# check if user has spawned more than 1 proc if spawned more than 1
# kill all user spawned processes 
if [[ "$(ps ax | grep $(echo ${USERS[$F]})|grep -v grep|wc -l)" -gt "1" ]]; then 

for j in `ps ax |grep $(echo ${USERS[$F]})|grep -v grep |awk '{ print $1 }'`; do
kill -9 $j;
done

fi

done

# get cpu time used by each user logged in efflab
for D in $(ps aux|egrep " <.*>$" | awk '{ print $3 }'); do
((N++));
USERS_CPU_USAGE[$N]="$(echo $D|sed -e 's#\.# #g'|awk '{ print $1 }')";
done

# if it equals more than 50 percent of cpu time then kill the problematic proc
for J in $(seq 0 $(ps ax |egrep " <.*>$" |wc -l)); do
((Y++));
if [[ "$(echo ${USERS_CPU_USAGE[$Y]})" -gt "50" ]]; then

if [ -n "${USERS_CPU_USAGE[$Y]}" ] && [ -n "${USERS[$Y]}" ]; then
PID_TO_KILL=`ps ax |grep " <.*>$"|grep -i $(echo ${USERS[$Y]}) |awk '{ print $1 }'`;
kill -9 ${PID_TO_KILL};
fi

fi
done
