Force a cleanup before exiting

This commit is contained in:
Andrea Dell'Amico 2021-06-25 19:11:02 +02:00
parent 52eb4dd1b0
commit 4fd1bb4946
Signed by: andrea.dellamico
GPG Key ID: 147ABE6CEB9E20FF
1 changed files with 11 additions and 11 deletions

View File

@ -3,7 +3,6 @@
# TODO:
# - print nagios friendly output into a file
# - kill rsession processes older than N days
#
set -e
set -o pipefail
@ -23,31 +22,31 @@ OUT_DIR=$( mktemp -d -t kill-rogue-jobs.XXXXXXXXXX )
USER_PROCS_LIST=$OUT_DIR/proclist
USER_PROCS_PARENTS=$OUT_DIR/parents
trap "eval logger '$CMD_NAME: trap intercepted, exiting.' ; cleanup" SIGHUP SIGINT SIGTERM
trap 'eval logger "$CMD_NAME: trap intercepted, exiting." ; cleanup' SIGHUP SIGINT SIGTERM
function cleanup() {
rm -fr $OUT_DIR
rm -fr "$OUT_DIR"
}
function find_rogue_processes() {
eval logger '$CMD_NAME: find_rogue_processes for user $USER_N'
ps -edaf | grep rsession | grep -v grep | grep ${USER_N} | awk '{ print $3 }' | uniq > $USER_PROCS_PARENTS
ps -edaf | grep rsession | grep -v grep | grep ${USER_N} | awk '{ print $2 }' | uniq > $USER_PROCS_LIST
ps -edaf | grep rsession | grep -v grep | grep "${USER_N}" | awk '{ print $3 }' | uniq > "$USER_PROCS_PARENTS"
pgrep -U "${USER_N}" rsession > "$USER_PROCS_LIST"
for parent in $( cat $USER_PROCS_PARENTS ) ; do
grep -v $parent $USER_PROCS_LIST > $USER_PROCS_LIST.tmp
mv $USER_PROCS_LIST.tmp $USER_PROCS_LIST
grep -v "$parent" "$USER_PROCS_LIST" > $USER_PROCS_LIST.tmp
mv $USER_PROCS_LIST.tmp "$USER_PROCS_LIST"
done
USER_PROCS_TO_KILL=$( cat $USER_PROCS_LIST )
USER_PROCS_TO_KILL=$( cat "$USER_PROCS_LIST" )
}
function exterminate() {
eval logger '$CMD_NAME: exterminate killing user $USER_N processes'
for pid in $( echo $USER_PROCS_TO_KILL ) ; do
kill -15 $pid
for pid in $( echo "$USER_PROCS_TO_KILL" ) ; do
kill -15 "$pid"
done
}
NUM_CPUS=$( grep processor /proc/cpuinfo | wc -l )
NUM_CPUS=$( grep -c processor /proc/cpuinfo )
ALLOWED_THREADS=$(( $NUM_CPUS - 1 ))
TOTAL_MEM=$( grep MemTotal /proc/meminfo | awk '{ print $2 }' )
ALLOWED_USED_MEM=$(( $TOTAL_MEM - $SPARE_MEM ))
@ -69,6 +68,7 @@ for USER_N in $( echo $USERS_SESSIONS ) ; do
fi
done
cleanup
trap cleanup EXIT
exit 0