AFP548

softwareupdate scripting

Hey, all, Anyone out there got any automated softwareupdate scripts they want to share? Here's one I wrote. Not even remotely pretty, and all are welcome to dissect it. What can I say? I'm a bash rookie... A forewarning about the script, though. This script is kicked off via cron every night at 1:15 AM. We've got 1500 OS X-capable Macs, and each school has pretty limited bandwidth. As such, my thought was to randomize the day the script runs softwareupdate (statistically giving softwareupdate a 20% chance of running each night). Also, to try to prevent a bunch of machines hammering the network all at the same time, there's a section that randomizes (within an hour) when softwareupdate actually kicks off. It works, it's ugly, but hey... It works. If you do end up using this script, I only ask that you give credit where credit's due. Happy scripting, all! #!/bin/sh # This script attempts to call softwareupdate, checks if reboots are necessary, # then runs the softwareupdate command, rebooting if necessary. If reboots # are not necessary, it exits after the install. # Written by: # Dave Bruhn # Durham Public School District # Durham, NC # Rewritten slightly by Eric Frost, IBM, Inc. # Variables and such tmp_file=/tmp/softwareupdate.$$ trap 'rm -rf $tmp_file' EXIT reboot="" found_updates="" offset_time="" random_day="" # Bandwidth is at a premium. Let's play the lottery and see if updates run today. RANGE=5 random_day=$RANDOM let "random_day %= $RANGE" echo "Random number is $random_day." if [ $random_day != "3" ] then echo "Today is not the day. Bye-bye." exit 0 fi # Clocks tend to get skewed. Let's clean this up. /usr/sbin/ntpdate -u wait # Need to offset the start time of this script. # First, generate random number. This will be number of seconds. RANGE=3600 offset_time=$RANDOM let "offset_time %= $RANGE" # Let the script sleep for the offset time so we don't hammer our bandwidth echo "Software Update will sleep for $offset_time seconds. Please hold." sleep $offset_time # And we're off. Run software update and wait until done. /usr/sbin/softwareupdate -l > $tmp_file wait reboot=$(/usr/bin/grep "restart" $tmp_file | /usr/bin/wc -w) echo "Restart is $reboot" found_updates=$(/usr/bin/grep "found the following" $tmp_file | /usr/bin/wc -w) echo "Updates found is $found_updates" /bin/rm -rf $tmp_file if [ $found_updates = "0" ] then echo "No updates found. Exiting." else if [ $reboot = "0" ] then echo "Updates found, but no reboot required. Installing now." /usr/sbin/softwareupdate -i --all else echo "Updates found, reboot required. Installing now." /usr/sbin/softwareupdate -i --all wait /sbin/reboot fi fi exit 0
Exit mobile version