Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Resets a specified guest account, or all guest accounts, to the state of
- # the "guest" user, to cleanup disk space and stale processes.
- # Users who are logged in will not be cleaned up. Use the "-f" option to
- # forcibly log them out first.
- # abort on error
- set -e
- if [ "$1" = "-f" ]; then
- force=yes
- fi
- all_users=`getent passwd | sed -e 's/:.*//'`
- for i in $all_users; do
- groups=`groups $i | sed -e 's/.* : //'`
- for g in $groups; do
- if [ "$g" = "students" ]; then
- guest_users="$guest_users $i"
- break
- fi
- done
- done
- do_users="$guest_users"
- if [ -n "$1" ]; then
- do_users="$*"
- fi
- for i in $do_users; do
- for g in $guest_users; do
- if [ "$i" = "$g" ]; then
- is_guest=yes
- break
- fi
- done
- if [ -z "$is_guest" ]; then
- echo "$i is not a guest!"
- exit 2
- fi
- if who | grep -q "^$i "; then
- echo -n "$i is logged in! "
- if [ -n "$force" ]; then
- echo "killing session"
- gnome-session-save --force-logout $i
- else
- echo "skipping. Use -f to kill their session."
- continue
- fi
- fi
- echo
- if killall -0 -i $i; then
- echo -n "$i has processes running!"
- if [ -n "$force" ]; then
- echo "killing them"
- killall -9 -u $i
- else
- echo "skipping. Use -f to kill their processes."
- continue
- fi
- fi
- do_users_loggedout="$do_users_loggedout $i"
- done
- for i in $do_users_loggedout; do
- home=`getent passwd $i | cut -d: -f6`
- # rsync -a --delete --exclude '/Documents' ~student-template/ $home
- rsync -a --delete ~student-template/ $home
- if [ ! -d "$home/Documents" ]; then
- mkdir $home/Documents
- fi
- chown -R $i $home
- done
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement