Skip to main content

I've been playing with some more robust ways to run softwareupdate than the built in implementation in Casper and came up with this. Modify as you like to make it work for your environment, it currently only works with 10.8 because 10.7 uses a slightly different structure for index.plist (and I just didn't have time to modify for it). Basically it checks if you are on 10.8, then if you have any updates, and if you do and they requires a restart, download them all and modify the proper files so that when someone does go to Apple - Restart, it'll bring up the built in Apple environment to show them a pretty progress bar on how their updates are going, and if there are updates but none of them require a restart, install them silently. This can be used in conjunction with a Policy so that you can warn users that it's running, that if it finds updates that require restart that it'll install those updates when they restart the computer, etc. etc. etc.



Enjoy!



#!/bin/bash

SWUL=/usr/sbin/softwareupdate -l | /usr/bin/awk '{printf "%s", $0}'
SWULER=/usr/sbin/softwareupdate -l 2>&1 | /usr/bin/head -1
NoRestartUpdates=/usr/sbin/softwareupdate -l | /usr/bin/grep -v restart | /usr/bin/grep -B1 recommended | /usr/bin/grep -v recommended | /usr/bin/awk '{print $2}' | /usr/bin/awk '{printf "%s ", $0}'
osvers=sw_vers -productVersion | awk -F. '{print $2}'

if [[ $osvers -eq 7 ]]; then
/bin/echo "Script only for 10.8 ONLY"
exit 1
elif [ "$SWULER" == "No new software available." ]; then
/bin/echo "$SWULER"
exit 1
elif [[ "$SWUL" == *"[restart]"* ]]; then
echo "Installing Updates that require Restart"
/usr/bin/sudo /usr/sbin/softwareupdate -d -a
/usr/libexec/PListBuddy -c "Copy CompletedProducts InstallAtLogout" /Library/Updates/index.plist
/usr/bin/touch /var/db/.SoftwareUpdateAtLogout
/bin/chmod og-r /var/db/.SoftwareUpdateAtLogout
/usr/libexec/PListBuddy -c "Add -RootInstallMode STRING YES" /var/db/.SoftwareUpdateOptions
/usr/libexec/PListBuddy -c "Add -SkipConfirm STRING YES" /var/db/.SoftwareUpdateOptions
/bin/chmod og-r /var/db/.SoftwareUpdateOptions
elif [[ "$SWUL" == *"[recommended]"* ]]; then
/bin/echo "Installing Updates that does not require Restart"
/usr/bin/sudo /usr/sbin/softwareupdate -i $NoRestartUpdates
fi

exit 0

@UESCDurandal @chop I am too getting the same error you were getting when installing updates. Did you manage to find a fix for this?


I've revisited this regarding the



Software Update.app (0)[1595]: Software Update.app: None of the keys registered for post-logout install () are available


error i mentioned above.
Turns out that kickstarting softwareupdated makes it work for me on Mavericks and Yosemite.
I've added



launchctl unload /System/Library/LaunchDaemons/com.apple.softwareupdated.plist
launchctl load /System/Library/LaunchDaemons/com.apple.softwareupdated.plist


to the end of the script.


msimpson - did you implement this script without making any changes to it when testing on 10.8, 10.9 and 10.10 ?


msimpson's script was implemented as a run-on-demand from Self Service. Limitations placed on the script to only allow running 10.8.x, 10.9.x, 10.10.x



A slight modification was made to the script at the beginning for removal of our internal update server plist we added to earlier mac builds.
Another difference is slight formatting and use of "`" - probably because the original script was posted as a quote and not script, perhaps breaking the script? HTML does funny things to scripts if not properly attached to a forum post. It breaks.



Below is verbatim what we use.



#!/bin/bash

## Remove swupdate preferences to use default server
jamf removeSWUSettings
rm /Library/Preferences/com.apple.SoftwareUpdate.plist
rm /Library/Managed Preferences/com.apple.SoftwareUpdate.plist
##

SWUL=`/usr/sbin/softwareupdate -l | /usr/bin/awk '{printf "%s", $0}'`
SWULER=`/usr/sbin/softwareupdate -l 2>&1 | /usr/bin/head -1`
NoRestartUpdates=`/usr/sbin/softwareupdate -l | /usr/bin/grep -v restart | /usr/bin/grep -B1 recommended | /usr/bin/grep -v recommended | /usr/bin/awk '{print $2}' | /usr/bin/awk '{printf "%s ", $0}'`
osvers=`sw_vers -productVersion | awk -F. '{print $2}'`

if [[ $osvers -eq 7 ]]; then
/bin/echo "Script only for 10.8 ONLY"
exit 1
elif [ "$SWULER" == "No new software available." ]; then
/bin/echo "$SWULER"
exit 1
elif [[ "$SWUL" == *"[restart]"* ]]; then
echo "Installing Updates that require Restart"
/usr/bin/sudo /usr/sbin/softwareupdate -d -a
/usr/libexec/PListBuddy -c "Copy CompletedProducts InstallAtLogout" /Library/Updates/index.plist
/usr/bin/touch /var/db/.SoftwareUpdateAtLogout
/bin/chmod og-r /var/db/.SoftwareUpdateAtLogout
/usr/libexec/PListBuddy -c "Add -RootInstallMode STRING YES" /var/db/.SoftwareUpdateOptions
/usr/libexec/PListBuddy -c "Add -SkipConfirm STRING YES" /var/db/.SoftwareUpdateOptions
/bin/chmod og-r /var/db/.SoftwareUpdateOptions
elif [[ "$SWUL" == *"[recommended]"* ]]; then
/bin/echo "Installing Updates that does not require Restart"
/usr/bin/sudo /usr/sbin/softwareupdate -i $NoRestartUpdates
fi

exit 0

This is a fantastic approach, and I really appreciate you sharing this innovative solution for managing software updates more efficiently. Your script is a clever workaround that significantly enhances the update process on OS X 10.8, making it more seamless for users and administrators alike. Great job!


Reply