Skip to content

Instantly share code, notes, and snippets.

@wirewc
Last active July 12, 2018 23:58
Show Gist options
  • Save wirewc/683a01dd1811383931245a7e015e6241 to your computer and use it in GitHub Desktop.
Save wirewc/683a01dd1811383931245a7e015e6241 to your computer and use it in GitHub Desktop.
Closes idle X2Go sessions that are open over 1 week.
#!/bin/bash
LIMIT_DAYS=7
TIME_ZONE=$(date +%Z)
DEBUG=false
for session in $(x2golistsessions_root); do
#Get state
state=$(echo $session|awk -F \| '{print $5}')
if [[ $DEBUG = true ]]; then
echo "State: $state"
fi
# If the state is 'S' for suspended, then continue.
if [[ $state == "S" ]]; then
# First get the idle day of the session
idledate=$(echo $session|awk -F \| '{print $11}';)
idleseconds=$(date -d $idledate +%s)
# Get Session ID from of connection, if there are a lot of connections then move to terminiation if statement.
SID=$(echo $session|awk -F \| '{print $2}')
# Current date in seconds
now=$(date +%s)
days=$(( ($now-$idleseconds) /60/60/24)) # Grab an integer of days run.
if [[ $DEBUG = true ]]; then
echo "Session: $session"
echo "Day: $idledate"
echo "Start Seconds: $idleseconds"
echo "Days instance has existed: $days"
echo "SID: $SID"
fi
# terminate if days == or greater than $LIMIT_DAYS
if [[ $days -ge $LIMIT_DAYS ]]; then
if [[ $DEBUG = true ]]; then
echo "Terminating session $SID that is $days days old"
fi
x2goterminate-session $SID
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment