Skip to main content

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

Nic-Automox
  • Former Automox Employee
  • 832 replies
  • April 16, 2020

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.


jagmeet.sidhu

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


Nic-Automox
  • Former Automox Employee
  • 832 replies
  • April 21, 2020

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.


  • Author
  • Novice
  • 12 replies
  • April 21, 2020

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.


Nic-Automox
  • Former Automox Employee
  • 832 replies
  • April 21, 2020

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


Forum|alt.badge.img
  • Automox Employee
  • 18 replies
  • April 21, 2020

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

  • Author
  • Novice
  • 12 replies
  • April 21, 2020

@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.


  • Novice
  • 16 replies
  • November 25, 2020

How can I do this with Ubuntu?


  • Novice
  • 38 replies
  • December 8, 2020

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.


Nic-Automox
  • Former Automox Employee
  • 832 replies
  • December 8, 2020

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:


  • Novice
  • 35 replies
  • May 27, 2021

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


jack.smith
Forum|alt.badge.img+1
  • All Star
  • 168 replies
  • May 27, 2021

image

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


jack.smith
Forum|alt.badge.img+1
  • All Star
  • 168 replies
  • May 27, 2021

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

  • Novice
  • 35 replies
  • May 28, 2021

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


jack.smith
Forum|alt.badge.img+1
  • All Star
  • 168 replies
  • June 2, 2021

@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.


Nic-Automox
  • Former Automox Employee
  • 832 replies
  • June 2, 2021

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?


jack.smith
Forum|alt.badge.img+1
  • All Star
  • 168 replies
  • June 2, 2021

I noticed at 11.2 Big Sur and has persisted through 11.3


  • Author
  • Novice
  • 12 replies
  • August 24, 2021

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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings