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.


18 replies

Userlevel 7

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.

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

Userlevel 7

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 🙂


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.

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.

Userlevel 7

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

Badge

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.

Userlevel 1

How can I do this with Ubuntu?

Userlevel 4

Is there a similar script for Windows users? I found a couple of Worklets to prompt and reboot, but not one to gently notify them to do it.

Userlevel 7

You could give this one a try. It asks them to reboot, but they can dismiss it and it doesn’t come back until the worklet runs again:


Userlevel 4

Does anyone have a screenshot of how this looks on the users end?

Userlevel 5
Badge +1

image


Recommend going with an icon size of 96x96 or higher here. Started with 32x32 and not a good quality output.

Userlevel 5
Badge +1

Trying to better understand how this works. Automox shows reboot required, the worklet is running and showing compliant. I’ve been manually running based on when I see Automox show a required reboot as I’ve not had success getting this to properly detect reboots as required.


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

How does this pick up the restart? I can’t see anything obvious that will detect restart

Userlevel 5
Badge +1

@Nic do you happen to know the evaluation is still working to detect a reboot? I’ve not had success detecting reboots on Mac OS X.

Userlevel 7

I haven’t played with it in a while, but to my knowledge nothing has changed about how macOS reports pending reboots or how Automox handles that. @tim.lee - anything change in that department that you know of?


@jack.smith - what version are you on that it isn’t detecting the pending reboots?

Userlevel 5
Badge +1

I noticed at 11.2 Big Sur and has persisted through 11.3

I do think this is now broken. We are not getting the prompt on our devices either.

Reply