Advertisement
Guest User

Xubuntu 14.04 xrdp disconnected session cleanup script

a guest
Aug 4th, 2014
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.35 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # This script cleans up disconnected xrdp+Xvnc sessions to free up memory
  4. # Xvnc processes with no established connections are assumed inactive and terminated
  5. # Running this script periodically will clean up remaining sessions as users disconnect
  6.  
  7. echo "`date +\"%d.%m.%Y %T\"` xrdp session cleanup script running"
  8.  
  9. XVNC_CONN=`netstat -ap --numeric-ports | grep -E "(tcp|udp)" | grep Xvnc`
  10. ALL_SESSIONS=`printf '%s\n' "${XVNC_CONN[@]}" | grep LISTEN`
  11. ACT_SESSIONS=`printf '%s\n' "${XVNC_CONN[@]}" | grep ESTABLISHED`
  12.  
  13. # Extract PIDs for all Xvnc processes that are listening
  14. IFS=$'\n'
  15. for i in ${ALL_SESSIONS[@]}; do
  16. #       echo $i
  17.         [[ "$i" =~ [0-9]+/Xvnc ]] && [[ "$BASH_REMATCH" =~ [0-9]+ ]] && PID=$BASH_REMATCH
  18. #       echo $PID
  19.         PIDS+=("$PID")
  20.         ACTIVE[$PID]='0'
  21. #       echo ${ACTIVE[$PID]}
  22. #       echo
  23. done
  24.  
  25. # Extract PIDs for Xvnc processes with established connections, mark as active
  26. for i in ${ACT_SESSIONS[@]}; do
  27.         [[ "$i" =~ [0-9]+/Xvnc ]] && [[ "$BASH_REMATCH" =~ [0-9]+ ]] && PID=$BASH_REMATCH
  28. #       echo $PID
  29.         ACTIVE[$PID]='1'
  30. done
  31.  
  32. # Kill all inactive Xvnc processes to free memory
  33. for i in ${PIDS[@]}; do
  34.         [[ "${ACTIVE[$i]}" == "1" ]] && echo "PID $i is active"
  35.         [[ "${ACTIVE[$i]}" == "0" ]] && echo "PID $i has no active connections, killing" && kill $i
  36. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement