I think it might be better to check if updates require a restart before installing them. If the updates require a restart and there is a user logged in, you could prompt the user to make a choice:
- Agree to the restart and install updates
- Decline the restart and not install updates
This would also allow you to install updates that do not require a restart without bothering the user. It may require some tweaking depending on exactly what/when/how you want things to happen in your environment. You could install updates, then present the user with a jamfHelper with a timer. You could follow-up on systems after a certain point with a version of this script that does not give them an option to cancel/decline but instead just waits for them to acknowledge that a restart will occur once they click OK. Lots of possibilities.
#!/bin/bash
#---VARIABLES---------------------------------------------------------------------------
JAMFHELPER="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"
ICON="/System/Library/CoreServices/Software Update.app/Contents/Resources/SoftwareUpdate.icns"
#---FUNCTIONS---------------------------------------------------------------------------
#---------------------------------------------------------------
# getConsoleUsername
# Returns the current console user. An empty return means no one
# is logged in.
#---------------------------------------------------------------
getConsoleUsername()
{
/usr/bin/python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser
user = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]
user = [user,""][user in [u"loginwindow", None, u""]]
print(user)'
}
#---------------------------------------------------------------
# promptRestartMessage
# Prompts user to acknowledge that a restart is required to
# finish installing software updates
#---------------------------------------------------------------
promptRestartMessage()
{
local _window_type _title _heading _description _button1 _default_button
_window_type="utility"
_title="Apple Software Updates"
_heading="Restart Required"
_description="Your computer needs to install Apple software updates that require a restart to complete. Please save all work and click Restart."
_button1="Restart"
_button2="Cancel"
_default_button="1"
_cancel_button="2"
echo "Prompting user to restart"
"$JAMFHELPER"
-windowType "$_window_type"
-title "$_title"
-icon "$ICON"
-heading "$_heading"
-description "$_description"
-button1 "$_button1"
-button2 "$_button2"
-defaultButton "$_default_button"
-cancelButton "$_cancel_button"
}
#---------------------------------------------------------------
# displayRestartReminder
# Displays a persistent dialog as a reminder to the user that
# their system will be restarting to install updates soon
#---------------------------------------------------------------
displayRestartReminder()
{
local _window_type _window_position _title _description
_window_type="hud"
_window_position="ur"
_title="Restart Pending"
_description="Apple software updates are being installed and your system will restart soon."
echo "Displaying restart reminder"
"$JAMFHELPER"
-windowType "$_window_type"
-windowPosition "$_window_position"
-lockHUD
-icon "$ICON"
-title "$_title"
-description "$_description" &
}
#---START SCRIPT------------------------------------------------------------------------
echo "Checking if a restart is required"
# Check if any updates require a restart to complete. The '[restart]' string exists on
# lines for updates that require a restart. The redirect 2>&1 is used to ensure all
# output from the softwareupdate command is sent the same way for the if evaluation
# because softwareupdate uses stderr for some reason for non errors (Don't ask me why).
# If no updates require a restart, all updates will install without user approval.
if [[ "$(/usr/sbin/softwareupdate --list 2>&1)" =~ '[restart]' ]]; then
echo "Restart is required"
# Check if a user is logged in to be able to accept or decline the restart.
# If no user is logged in, the updates will install without user approval.
if [ -n "$(getConsoleUsername)" ]; then
echo "A user is logged in"
# Prompt the user to accept the restart or cancel the updates
if promptRestartMessage; then
echo "User accepted restart"
# Display a window reminding the user that a restart will occur soon
displayRestartReminder
# Exit clean if the user chose to not accept the restart
else
echo "User did not accept restart"
exit 0
fi
# Log the fact that no user is logged in
else
echo "No one is logged in"
fi
# Log the fact that no updates require a restart
else
echo "No updates require a restart"
fi
# Install all updates with the --restart flag which will
# restart the system if any updates require a restart
/usr/sbin/softwareupdate --install --all --restart
exit 0