Skip to content

Instantly share code, notes, and snippets.

@AndrewWCarson
Last active May 20, 2019 20:17
Show Gist options
  • Save AndrewWCarson/2d54bf5c2cec23693251a1234eaeb04e to your computer and use it in GitHub Desktop.
Save AndrewWCarson/2d54bf5c2cec23693251a1234eaeb04e to your computer and use it in GitHub Desktop.
Applies any available updates and prompts any logged-in user to restart.
#!/bin/bash
# Modify these strings to change the verbiage in the badge notification.
title="Updates Applied"
description="macOS updates have been applied to your device. Please restart to complete the process."
acceptText="Restart"
closeText="Later"
if (( $(softwareupdate -l 2>&1 | grep -c ' *') > 0 )); then
softwareupdate -ia
username=$(/usr/bin/python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");')
if [[ "$username" == "" ]]; then
echo "No one logged in. Let's restart after updating"
(sleep 5 && reboot) &> /dev/null &
exit 0
elif /Library/Addigy/macmanage/MacManage.app/Contents/MacOS/MacManage action=notify title="${title}" description="${description}" closeLabel="${closeText}" acceptLabel="${acceptText}"; then
echo "User:$username accepted restart."
sudo -u "$username" osascript -e 'tell app "loginwindow" to «event aevtrrst»'
exit 0
else
echo "User:$username delayed restart."
exit 0
fi
else
echo "No available updates for this device."
exit 0
fi
@AndrewWCarson
Copy link
Author

Lots of people have asked me for help with automatically updating macOS devices and prompting the restart. I don't recommend this model, so don't blame me if this breaks things.

  1. Check for updates.
  2. Apply updates.
  3. If no one's logged in, then restart.
  4. Otherwise, prompt any logged in user to restart. And pray.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment