Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # This script cleans up disconnected xrdp+Xvnc sessions to free up memory
- # Xvnc processes with no established connections are assumed inactive and terminated
- # Running this script periodically will clean up remaining sessions as users disconnect
- echo "`date +\"%d.%m.%Y %T\"` xrdp session cleanup script running"
- XVNC_CONN=`netstat -ap --numeric-ports | grep -E "(tcp|udp)" | grep Xvnc`
- ALL_SESSIONS=`printf '%s\n' "${XVNC_CONN[@]}" | grep LISTEN`
- ACT_SESSIONS=`printf '%s\n' "${XVNC_CONN[@]}" | grep ESTABLISHED`
- # Extract PIDs for all Xvnc processes that are listening
- IFS=$'\n'
- for i in ${ALL_SESSIONS[@]}; do
- # echo $i
- [[ "$i" =~ [0-9]+/Xvnc ]] && [[ "$BASH_REMATCH" =~ [0-9]+ ]] && PID=$BASH_REMATCH
- # echo $PID
- PIDS+=("$PID")
- ACTIVE[$PID]='0'
- # echo ${ACTIVE[$PID]}
- # echo
- done
- # Extract PIDs for Xvnc processes with established connections, mark as active
- for i in ${ACT_SESSIONS[@]}; do
- [[ "$i" =~ [0-9]+/Xvnc ]] && [[ "$BASH_REMATCH" =~ [0-9]+ ]] && PID=$BASH_REMATCH
- # echo $PID
- ACTIVE[$PID]='1'
- done
- # Kill all inactive Xvnc processes to free memory
- for i in ${PIDS[@]}; do
- [[ "${ACTIVE[$i]}" == "1" ]] && echo "PID $i is active"
- [[ "${ACTIVE[$i]}" == "0" ]] && echo "PID $i has no active connections, killing" && kill $i
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement