System needs a reboot prompting script?

I wonder if its possible to make a mac script that would check if system is pending a reboot and then push user notifications to do it, not necessary to have an option for user to do it from the message, just want to alert user they need to reboot…repeatedly…until they hopefully do.

1 Like

Off the top of my head that seems feasible. Let me ping @tim.lee who is our Mac expert to confirm that something like that can be done and then I can take a crack at putting something together.

1 Like

Looking forward to your response @Nic. Have a use case if doable.

Ok I’ve made a first pass at this one. It doesn’t have deferrals built in yet, but it does detect which Macs need reboot and then pops up a dialog box for the user to reboot now or defer. You can set the worklet schedule to then bug them as often as you like :slight_smile:

Evaluation:

if [[ -d "/Volumes/macOS Base System" || -d "/Volumes/OS X Base System" ]]; then
	exit 1
else
	exit 0
fi

Remediation:

#!/bin/bash
declare -i notificationInterval=30
title="Reboot required"
message="Your computer needs to be rebooted to complete patch updates"
console_user=$(scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }')
console_user_gui=$(dscl . read /Users/$console_user UniqueID | awk '{print $2}')
launchctl asuser "${console_user_gui}" osascript <<EOT
display dialog "${message}" with title "${title}" buttons {"Later", "Reboot Now"} default button {"Reboot Now"}
if the button returned of the result is "Reboot Now" then 
	tell application "Finder"
    	restart
    end tell
end if
EOT

Big thanks to @tim.lee for helping me pull this together and providing the wrapper code to run osascript as the currently logged in user.

@awhitman is also working on an improved version of this with actual deferrals built into the code, so stay tuned for that.

1 Like

This is great! I will keep you posted on how testing goes. I have changed the default button to “later” as I want it to fail to no action rather than fail to an unexpected reboot. Fingers crossed.

Good call, don’t want them just hitting enter and then not realizing they have a reboot coming.

Here’s the above code, but you can optionally upload an icon (.icns) file to the worklet and it will inject it into the message. If anyone needs to make an icns from an existing company logo image, there’s a greate code snippet here.

#!/bin/bash


title="message title"
message="message body"
iconFile="icon.icns" #optional: upload a icon file to this worklet 



iconFilePath="$(pwd | sed -E 's/^\///g;s/\//:/g'):${iconFile}"

console_user=$(scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }')
console_user_gui=$(dscl . read /Users/$console_user UniqueID | awk '{print $2}')

if [[ -n "${iconFile}" ]]; then
	addIcon="with icon alias \"${iconFilePath}\""
	echo "${addIcon}"
fi

launchctl asuser "${console_user_gui}" osascript << EOT
display dialog "${message}" with title "${title}" buttons {"Reboot Now", "Later"} default button {"Later"} ${addIcon}
if the button returned of the result is "Reboot Now" then 
	tell application "Finder"
    	restart
    end tell
end if
EOT

@Nic, So far the code works better than expected. I would love to see a screenshot of what it might look like with a icon, not sure if its worth the extra effort right now.

Thank you @Nic and @tim.lee for putting this together. It really ties up the whole user experience to the need to keep systems updated nicely by politely nudging end-users in the right direction.

1 Like