Advertisement
Corbin22

ClearGuests.sh

Mar 29th, 2016
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.58 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Resets a specified guest account, or all guest accounts, to the state of
  4. # the "guest" user, to cleanup disk space and stale processes.
  5. # Users who are logged in will not be cleaned up. Use the "-f" option to
  6. # forcibly log them out first.
  7.  
  8. # abort on error
  9. set -e
  10.  
  11. if [ "$1" = "-f" ]; then
  12.     force=yes
  13. fi
  14.  
  15. all_users=`getent passwd | sed -e 's/:.*//'`
  16.  
  17. for i in $all_users; do
  18.     groups=`groups $i | sed -e 's/.* : //'`
  19.     for g in $groups; do
  20.         if [ "$g" = "students" ]; then
  21.             guest_users="$guest_users $i"
  22.             break
  23.         fi
  24.     done
  25. done
  26.  
  27. do_users="$guest_users"
  28.  
  29. if [ -n "$1" ]; then
  30.     do_users="$*"
  31. fi
  32.  
  33. for i in $do_users; do
  34.     for g in $guest_users; do
  35.         if [ "$i" = "$g" ]; then
  36.             is_guest=yes
  37.             break
  38.         fi
  39.     done
  40.  
  41.     if [ -z "$is_guest" ]; then
  42.         echo "$i is not a guest!"
  43.         exit 2
  44.     fi
  45.  
  46.     if who | grep -q "^$i "; then
  47.         echo -n "$i is logged in! "
  48.         if [ -n "$force" ]; then
  49.             echo "killing session"
  50.             gnome-session-save --force-logout $i
  51.         else
  52.             echo "skipping. Use -f to kill their session."
  53.             continue
  54.         fi
  55.     fi
  56.  
  57.     echo
  58.  
  59.     if killall -0 -i $i; then
  60.         echo -n "$i has processes running!"
  61.         if [ -n "$force" ]; then
  62.             echo "killing them"
  63.             killall -9 -u $i
  64.         else
  65.             echo "skipping. Use -f to kill their processes."
  66.             continue
  67.         fi
  68.     fi
  69.  
  70.     do_users_loggedout="$do_users_loggedout $i"
  71. done
  72.  
  73. for i in $do_users_loggedout; do
  74.     home=`getent passwd $i | cut -d: -f6`
  75.     rsync -a --delete --exclude '/Documents' ~student-template/ $home
  76.     if [ ! -d "$home/Documents" ]; then
  77.         mkdir $home/Documents
  78.     fi
  79.     chown -R $i $home
  80. done
  81.  
  82. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement