Skip to content

Instantly share code, notes, and snippets.

@brettfreer
Last active May 23, 2016 08:40
Show Gist options
  • Save brettfreer/01210fdf918a1f6d8e360212580e5a50 to your computer and use it in GitHub Desktop.
Save brettfreer/01210fdf918a1f6d8e360212580e5a50 to your computer and use it in GitHub Desktop.
Auto-logout script for linux
#!/bin/sh
#
# linux_idleout.sh [idle time]
#
# Auto-logout process for telnet/ssh sessions
#
IDLE_TIME=180
LOG_FILE=/tmp/idle.out
EXEMPT_PROCESSES="app1|app2|app3|etc" # optional list of apps which can be excluded
# killproc user device
killproc()
{
user=$1
term=$2
ps -ft $term | grep $user | while read line
do
set $line
pid=$2
# try a gentle kill first
kill -15 ${pid} 2>/dev/null
sleep 5
# now use a hard kill if the process is still there
kill -9 ${pid} 2>/dev/null
done
}
#
# main start here
#
# check for optional passed idle time
if [ $# -eq 1 ]
then
IDLE_TIME=$1
fi
# scan the process table
ps -ef | egrep ${EXEMPT_PROCESSES} | while read line
do
# extract values from ps command
set $line
user=$1
term=$6
idle=`find /dev/${term} -mmin +${IDLE_TIME} -exec ls {} \; 2>/dev/null | wc -l`
if [ $idle -gt 0 -a $user != "tbm" -a $user != "root" -a $user != "aradmin" ]
then
date >>$LOG_FILE
echo $user $term >>$LOG_FILE
# send a warning to the user session
# use timeout in case the warning blocks
timeout --signal=SIGTERM 1m banner TimeOut >/dev/${term}
killproc $user $term
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment