Force a cleanup before exiting

master
Andrea Dell'Amico 3 years ago
parent 52eb4dd1b0
commit 4fd1bb4946
Signed by: andrea.dellamico
GPG Key ID: 147ABE6CEB9E20FF

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

Loading…
Cancel
Save