Skip to content

Instantly share code, notes, and snippets.

@makeittotop
Last active August 24, 2016 08:03
Show Gist options
  • Save makeittotop/4f2b2a6e618f9cc56d32732de8c7a1b2 to your computer and use it in GitHub Desktop.
Save makeittotop/4f2b2a6e618f9cc56d32732de8c7a1b2 to your computer and use it in GitHub Desktop.
log-out-idle-users
w uses the access time of the tty to determine how idle someone is, this is covered in idletime()
which stats the tty file and subtracts its atime from the current time. pkill can use a terminal as the filter
to kill processes.
So, you want to kill anyone who isn't root and idle for over 5 minutes?
for t in `w -h | grep -v '^root' | awk '{print $2}'`; do find /dev/$t -amin +5 -exec pkill -t $t \; ; done
What is going on here:
Use w with no headers to find all users
Filter out root
print out the ttys which is column 2
for each line, do a find to test the access time is more than 5 minutes
if it is, kill all processes on that terminal with pkill
You can configure Linux or Unix-like system to automatically log users out after a period of inactivity. Simply login as the root user and create a file called /etc/profile.d/autologout.sh, enter:
# vi /etc/profile.d/autologout.sh
Append the following code:
TMOUT=300
readonly TMOUT
export TMOUT
Save and close the file. Set permissions:
# chmod +x /etc/profile.d/autologout.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment