#!/bin/sh
# Log out users that has been idle  for more then $IDLEHOURS
# Andreas Henriksson <andreas@fatal.se>, 2007-05-10

IDLEHOURS=2

TMP=$(mktemp)

w -s -h | awk '{if (match($4, ":")) print $1,$2,$4}' > $TMP

LINE=1
for hours in $(cut -d' ' -f3 < $TMP | cut -d: -f1)
do
	if [ "$hours" -gt $IDLEHOURS ]
	then
		VICTIM=$(head -n $LINE < $TMP | tail -n 1)
		echo "---- victim: $VICTIM ----"
		TTY=$(echo $VICTIM | cut -d' ' -f2)
		for PID in $(ps ht $TTY | awk '{print $1}')
		do
			echo "killing pid $PID"
			#kill $PID
		done
	fi
	((LINE++))
done


rm $TMP

