Skip to content

Instantly share code, notes, and snippets.

@mnebuerquo
Created June 24, 2016 11:46
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mnebuerquo/e825530cf2bfd363b6c3cd82fe697d94 to your computer and use it in GitHub Desktop.
Save mnebuerquo/e825530cf2bfd363b6c3cd82fe697d94 to your computer and use it in GitHub Desktop.
place in /etc/cron.hourly and it will kill any idle vnc sessions
#!/bin/bash
# http://serverfault.com/a/767361/33170
displays=`ps aux | grep Xvnc | grep -v 'grep\|sed' | sed -r 's|.*(Xvnc :[0-9]*).*|\1|' | cut -d' ' -f 2`
limit=30
date
echo "Checking for inactive sessions!"
while read -r d; do
export DISPLAY=$d
idle=`xprintidle`
idleMins=$(($limit/1000/60))
if [[ $idleMins -gt $limit ]]; then
# http://superuser.com/questions/909400/killing-vnc-process-manually
echo "WARN Killing display $d because it is logged in for longer than ${limit}min (${idleMins}m)"
PID=$(pgrep -f "vnc $d")
echo "Killing $d ($PID)"
kill -HUP $PID
# http://linuxtoolkit.blogspot.com/2013/03/xrdpmmprocessloginresponse-login-failed.html
FNAME=$(echo $d | sed -e 's/:/X/g')
FILENAME="/tmp/.X11-unix/$FNAME"
echo "Removing session for $d ($FILENAME)"
rm -f $FILENAME
else
echo "INFO Display $d is still ok (${idleMins}m)"
fi
done <<< "$displays"
@maxwelleite
Copy link

maxwelleite commented Mar 22, 2017

Require to install xprintidle package (sudo apt-get install xprintidle).
But xprintidle is incompatible with tightvnc sessions. Get the error message: "screen saver extension not supported".
So only works for vnc server (vnc4server) ;-(

Need some code enhance. But works! 👍

@maxwelleite
Copy link

Checkout my own fork of this code with some fix and updates:
https://gist.github.com/maxwelleite/b352dd681b74e7cf09b236e7d40c1794

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment