Beginner scriptor in need of some assistance. I basically pieced this script together from two other scripts, it fails when I run this and works when separated.
This is the error I receive when running the script, which I'm guessing is a syntax error. Any help would be greatly appreciated!
Script exit code: 2
Script result: /Library/Application Support/JAMF/tmp/Delete User Profiles Older than 1 day: line 19: unexpected end of file
Script
#!/bin/bash
# Delete User Profiles in /Users. Exclude the 'Shared' account.
oldUsers=`find /Users -type d -mtime +1 -maxdepth 30 | cut -d"/" -f3`
for oldUsers in `ls /Users | grep -v Shared`
do
if [[ $oldUsers == `ls -l /dev/console | awk '{print $3}'` ]]; then
echo "Skipping user: $username (current user)"
else
echo "Removing user: $username"
# Optional, removes the account
dscl . delete /Users/$username
# Removes the user directory
rm -rf /Users/$username
fi
Best answer by mscottblake
@Poseiden That script I had deletes all users on the machine. I was running that in a lab environment. This is what I run when I need to delete accounts older than X days:
#!/bin/bash
# Modified 2015-03-11
# delete_inactive_users.sh
# Maintained at https://github.com/dankeller/macscripts
# by Dan Keller
#
# MIT License
#
#======================================
#
# Script to delete local user data that has not been accessed in a given time
# period.
#
# This script scans the /Users folder for the date last updated (logged in)
# and deletes the folder as well as the corresponding user account if it has
# been longer than the time specified. You can specify user folders to keep as
# well.
#
# User data not stored in /Users is not effected.
#
# Helpful for maintaing shared/lab Macs connected to an AD/OD/LDAP server.
#
#======================================
#----Variables----
# DEFAULT VALUE FOR "AGE" IS SET HERE
AGE=122 # Delete /Users/ folders inactive longer than this many days
# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 4 AND, IF SO, ASSIGN TO "AGE"
if [ "$4" != "" ]; then
AGE=$4
fi
KEEP=("/Users/Shared") # User folders you would like to bypass. Typically local users or admin accounts.
#--End variables--
### Delete Inactive Users ###
if [[ ${UID} -ne 0 ]]; then
echo "$0 must be run as root."
exit 1
fi
USERLIST=$(/usr/bin/find /Users -type d -maxdepth 1 -mindepth 1 -not -name "." -mtime +"${AGE}")
echo "Performing inactive user cleanup"
for a in ${USERLIST}; do
if ! [[ ${KEEP[*]} =~ "$a" ]]; then
echo "Deleting inactive (over ${AGE} days) account and home directory: $a"
# delete user
/usr/bin/dscl . delete "$a" > /dev/null 2>&1
# delete home folder
/bin/rm -r "$a"
continue
else
echo "SKIPPING $a"
fi
done
echo "Cleanup complete"
exit 0
@Poseiden That script I had deletes all users on the machine. I was running that in a lab environment. This is what I run when I need to delete accounts older than X days:
#!/bin/bash
# Modified 2015-03-11
# delete_inactive_users.sh
# Maintained at https://github.com/dankeller/macscripts
# by Dan Keller
#
# MIT License
#
#======================================
#
# Script to delete local user data that has not been accessed in a given time
# period.
#
# This script scans the /Users folder for the date last updated (logged in)
# and deletes the folder as well as the corresponding user account if it has
# been longer than the time specified. You can specify user folders to keep as
# well.
#
# User data not stored in /Users is not effected.
#
# Helpful for maintaing shared/lab Macs connected to an AD/OD/LDAP server.
#
#======================================
#----Variables----
# DEFAULT VALUE FOR "AGE" IS SET HERE
AGE=122 # Delete /Users/ folders inactive longer than this many days
# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 4 AND, IF SO, ASSIGN TO "AGE"
if [ "$4" != "" ]; then
AGE=$4
fi
KEEP=("/Users/Shared") # User folders you would like to bypass. Typically local users or admin accounts.
#--End variables--
### Delete Inactive Users ###
if [[ ${UID} -ne 0 ]]; then
echo "$0 must be run as root."
exit 1
fi
USERLIST=$(/usr/bin/find /Users -type d -maxdepth 1 -mindepth 1 -not -name "." -mtime +"${AGE}")
echo "Performing inactive user cleanup"
for a in ${USERLIST}; do
if ! [[ ${KEEP[*]} =~ "$a" ]]; then
echo "Deleting inactive (over ${AGE} days) account and home directory: $a"
# delete user
/usr/bin/dscl . delete "$a" > /dev/null 2>&1
# delete home folder
/bin/rm -r "$a"
continue
else
echo "SKIPPING $a"
fi
done
echo "Cleanup complete"
exit 0
@Poseiden I'm glad that works for you. I think the best thing about it is that to change the duration, you just need to change parameter 4 in your policy. Therefore the same script can be used in different policies where requirements are different.
@Poseiden I am new to Casper and the need use of scripts. Can you send me your "delete accounts older than X days" script with some normal data entered. I am having trouble following/figuring out how to enter the users in the "keep" area and if I need to adjust anything in the $4 area. Thanks
@gmillercmsd12 The script he's referencing can be found above. Copy that script into your JSS. Then you can change the KEEP variable to KEEP=("/Users/Shared" "/Users/AnotherUser"). You can change the default age with the AGE variable. That's in days. Alternatively, you can pass a number into parameter 4 in your policy to customize the delay before they are deleted.
Thanks for this, @mscottblake! Exactly what I was looking for to clean up these 128gb Airs with 100 student user folders on them without deleting frequent users.
I was just testing the script posted by@mscottblake and I'm not sure if I did something wrong. I have the script, changed it to 5 days to test deleting some older Users on a machine. The policy is set in Self Service and available with JSS login credentials matching jamf_admins or help_desk. I logged into the computer as a student standard account then launched Self Service. Logged into Self Service as a user in jamf_admin so I could see and run the policy. Once the policy ran it kept the users I wanted, Shared and local admin but it deleted all the local user accounts including the student one I was logged in as, I watched the Desktop items disappeared. I logged out of the user, logged in as local admin and checked System Preferences, the only accounts left were the ones in the script I excluded. What I was hoping is that it would leave the current logged in user alone. Maybe I misinterpreted the script but I would think the current logged in user would have a modified date of today so it would ignore current logged in user.
Is there a way to add "Users/~CurrentUser" as a user folder not to delete.
This is the script I'm using. The 14 in 'mtime +14' are the days. change that accordingly and accounts with a user ID less than of 1000 are local accounts are not deleted. Give this a shot.
#!/bin/sh
userList=`dscl . list /Users UniqueID | awk '$2 > 1000 {print $1}'`
echo "Deleting account and home directory for the following users..."
for a in $userList ; do
find /Users -type d -maxdepth 1 -mindepth 1 -not -name "*.*" -mtime +14 | grep "$a";
if [[ $? == 0 ]]; then
dscl . delete /Users/"$a"; #delete the account
rm -r /Users/"$a"; #delete the home directory
fi
done