script-user-reset-x2go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env bash
  2. # This script will reset x2go sessions to a working state
  3. # Use --all to reset all user X2Go sessions
  4. # Copyright Bryan C. Roessler
  5. parent="${BASH_SOURCE[0]}"
  6. parent=${parent%/*}
  7. [[ -f $parent/script-functions ]] && . "$parent"/script-functions || exit 1
  8. echo "This script supports one optional argument, a username (or --all for all users)"
  9. USER_MODE=0
  10. USER_LIST=()
  11. if [[ $EUID -gt 0 ]]; then
  12. echo "This script should normally be run as root (with sudo)"
  13. echo "Attempting to run in user mode"
  14. USER_MODE=1
  15. USER_LIST+=($(whoami))
  16. else
  17. if [[ $# -eq 1 ]]; then
  18. if [[ $1 == '--all' ]]; then
  19. for i in /home/*; do
  20. USER_LIST+=("${i##*/}")
  21. done
  22. else
  23. USER_LIST+=("$1")
  24. fi
  25. else
  26. prompt user
  27. USER_LIST+=("$user")
  28. unset user
  29. fi
  30. fi
  31. for user in "${USER_LIST[@]}"; do
  32. # Clean local user cache
  33. shopt -s nullglob
  34. caches=(/home/"$user"/.x2go/C-"$user"-* /home/"$user"/.xsession-x2go-*)
  35. shopt -u nullglob
  36. if [[ ${#caches} -gt 0 ]]; then
  37. ask_ok "Remove X2Go cache files for user $user?" &&
  38. rm -rf /home/"$user"/.x2go/C-"$user"-* &&
  39. echo "Removed: ${caches[*]} for user $user"
  40. fi
  41. # Clean X2Go sessions
  42. if (( USER_MODE )); then
  43. mapfile -t sessions < <(x2golistsessions | grep "$user"| cut -f2 -d'|')
  44. else
  45. mapfile -t sessions < <(x2golistsessions_root | grep "$user"| cut -f2 -d'|')
  46. fi
  47. if [[ ${#sessions} -gt 0 ]]; then
  48. ask_ok "Terminate X2Go sessions for user $user?" &&
  49. for session in "${sessions[@]}"; do
  50. x2goterminate-session "$session" &&
  51. echo "Terminated: $session for user $user"
  52. done
  53. fi
  54. done