File nxclean of Package nxcleanup
#!/bin/bash
#
# Usage: nxclean [-dh] USER
#
# - kills all processes of the user
# - removes all files in /tmp owned by the user
# - kills all processes matching 'nx' and $USER
#
# please use this script carefully
#
# Urs Beyerle
#
PATH=/sbin:/bin:/usr/bin:/usr/sbin
IFS=' '
### Function
function usage () {
cat <<EOF
Usage: nxclean [-dh] [USER]
Options:
-h: Print this help screen
-d: Debug output
* kills all processes of the user
* removes all files in /tmp owned by the user
* works only for root: kills all processes matching
'nx' and 'USER'
Root MUST give a username [USER] as argument. All other
users can run nxclean only without the [USER] argument.
Please use this script carefully. It will kill ALL of your
running processes and sessions on $HOSTNAME.
EOF
exit 1
}
### Read argument
dev_null="2>/dev/null"
if [ "$1" = "-d" ]; then
debug="-d"
shift
fi
[ "$1" = "-h" ] && usage
[ "$1" = "--help" ] && usage
my_username=$( id -un )
if [ "$my_username" = "root" ]; then
user=$1
else
[ "$1" != "" ] && usage
user=$my_username
fi
[ "$user" = "" ] && usage
### Main
echo
echo "I will do the following:"
echo "- find /tmp -user $user -exec rm -rf {} \;"
echo "- pkill -9 -u $user"
[ "$my_username" = "root" ] && echo "- kill all processes matching: ps wwaux | grep nx | grep $user"
echo
if [ "$my_username" = "root" ]; then
echo "This will kill all running processes and sessions of user $user on $HOSTNAME !"
else
echo "This will kill all your running processes and sessions on $HOSTNAME !"
fi
echo
echo -n "Do you want to continue (y/N)? "
read ans
echo
if [ "$ans" = "y" ]; then
[ "$debug" != "" ] && echo "Run in debug mode."
if [ "$debug" != "" ]; then
find /tmp -user $user -exec rm -rf {} \;
else
find /tmp -user $user -exec rm -rf {} \; 2>/dev/null
fi
if [ "$debug" != "" ]; then
pkill -9 -u $user
else
pkill -9 -u $user 2>/dev/null
fi
if [ "$my_username" = "root" ]; then
pids=$( ps wwaux | grep nx | grep $user | awk '{ print $2 }' )
for pid in $pids; do
if [ "$debug" != "" ]; then
kill $pid
else
kill $pid 2>/dev/null
fi
done
fi
fi