Skip to content

Instantly share code, notes, and snippets.

@dhulihan
Created February 18, 2023 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhulihan/4c65e868851660fb0d8bfa2d059e7967 to your computer and use it in GitHub Desktop.
Save dhulihan/4c65e868851660fb0d8bfa2d059e7967 to your computer and use it in GitHub Desktop.
Kill Old Tmux Sessions
# this script lists old tmux sessions and kills them to free up resources
TOO_OLD_THRESHOLD_SECS=604800 # 7 days
NOW=$((`date +%s`))
tmux ls -F '#{session_name} #{session_activity}' | while read -r LINE; do
SESSION_NAME=$(echo $LINE | awk '{print $1}')
LAST_ACTIVITY=$(echo $LINE | awk '{print $2}')
LAST_ACTIVITY_SECS_ELAPSED=$(($NOW - $LAST_ACTIVITY))
if [[ "$LAST_ACTIVITY_SECS_ELAPSED" -gt "$TOO_OLD_THRESHOLD_SECS" ]]; then
echo "${SESSION_NAME} is ${LAST_ACTIVITY_SECS_ELAPSED}s inactive, killing..."
tmux kill-session -t ${SESSION_NAME}
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment