Skip to content

Instantly share code, notes, and snippets.

@maxwelleite
Forked from mnebuerquo/kill-idle-vnc.sh
Last active May 20, 2018 01:29
Show Gist options
  • Select an option

  • Save maxwelleite/b352dd681b74e7cf09b236e7d40c1794 to your computer and use it in GitHub Desktop.

Select an option

Save maxwelleite/b352dd681b74e7cf09b236e7d40c1794 to your computer and use it in GitHub Desktop.

Revisions

  1. maxwelleite revised this gist May 20, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion kill-idle-vnc.sh
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ while read -r d; do
    exit
    fi

    idleMins=$((($idle/1000/60))
    idleMins=$(($idle/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)"
  2. maxwelleite revised this gist Mar 22, 2017. 1 changed file with 14 additions and 4 deletions.
    18 changes: 14 additions & 4 deletions kill-idle-vnc.sh
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,26 @@
    #!/bin/bash

    # http://serverfault.com/a/767361/33170
    # file-name: kill-idle-vnc.sh
    # Author: Maxwel Leite (http://needforbits.wordpress.com/)
    # Initial Author: Sherman Adelson (https://gist.github.com/mnebuerquo)
    # Description: Script to kill any idle vnc sessions (for disconnected xrdp sessions)
    # SRC: 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
    limit=30 # in minutes

    date
    echo "Checking for inactive sessions!"
    while read -r d; do
    export DISPLAY=$d
    idle=`xprintidle`
    idleMins=$(($limit/1000/60))

    if ! [ "$idle" -eq "$idle" ] 2>/dev/null
    then
    echo "INFO Nothing to do, no vnc sessions found!"
    exit
    fi

    idleMins=$((($idle/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)"
  3. @mnebuerquo mnebuerquo created this gist Jun 24, 2016.
    28 changes: 28 additions & 0 deletions kill-idle-vnc.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    #!/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"