How do I alert people if their Macbook turned on for more then x days?

DonCascone
New Contributor II

I've been using a policy executing a straight up restart option bound to a SCG that gathers every Macbook that hasn't been turned off for more then 10 days but I find the policy to be too invasive since it doesn't allow deferral as far as I know.
I've been searching if there is a way to just send a notification if somebody enters a SCG on their Macbook but I can't find anything of the sort and I'm not very good at scripting either.
Do you have any tips about it?
Thank you

4 ACCEPTED SOLUTIONS

Anusan16
New Contributor

Then you can use the Jamf Helper.  It will prompt a message on screen. Use this script on a policy then scope the SCG. Ensure you test to get the desired outcome.

#!/bin/bash

# Path to Jamf Helper
JAMF_HELPER="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"

# Message and options
HEADING="System Restart Required"
MESSAGE="To complete essential updates, please restart your device. You can choose to restart now or delay until later."

# Display the Jamf Helper window with two buttons
USER_CHOICE=$("$JAMF_HELPER" -windowType utility -title "$HEADING" -heading "$HEADING" -description "$MESSAGE" \
-icon "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns" \
-button1 "Restart Now" -button2 "Later" -defaultButton 1)

# Check the user's choice
if [ "$USER_CHOICE" == "0" ]; then
# Restart the device immediately
sudo shutdown -r now
else
# Postpone the restart and notify again in 1 hour (optional customization)
echo "User chose to restart later."
fi

View solution in original post

stutz
Contributor

@DonCascone Depends on how you want to notify your end users. 

swiftDialog:
I would use a policy, scope it to the SCG and build out a swiftDialog (https://github.com/swiftDialog/swiftDialog) script to inform users.  This provides a lot of custom options to display the notification in a professional way.  I believe you can also do a "macOS Notification" style alert as well.

If you don't want to deal with installing swiftDialog you can just use Jamf's built-in binaries to throw up a popup window and do the same thing as above minus the fancy window customizations.

Jamf Helper Window:

Screenshot 2024-10-30 at 7.54.24 AM.png

 

 

#!/bin/bash

# jamfHelper window
userChoice=$("/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper" -windowType "hud" -title "Reboot Notification" -heading "Please Reboot" -description "We noticied you haven't rebooted in awhile.  Please reboot to ensure your Mac is in proper working order." -icon "/System/Library/CoreServices/Diagnostics Reporter.app/Contents/Resources/AppIcon.icns" --iconSize 100 -button1 "REBOOT" -button2 "CLOSE" -defaultButton 0 -lockHUD -timeout 60 -countdown -countdownPrompt "" -alignCountdown center)

if [ "$userChoice" == "0" ]; then

	echo "User Clicked: Reboot or timer expired"
	shutdown -r now


else

	echo "User Clicked: Close"

fi

 

 

 


AppleScript Notification:

Screenshot 2024-10-30 at 7.53.23 AM.png

 

 

#!/bin/bash

/usr/bin/osascript <<APPLESCRIPT

display notification "You haven't rebooted in awhile.  Please do this at your earliest convenience." with title "Reboot Reminder"

APPLESCRIPT

 


As always run these scripts on a test machine before putting them into PROD.

View solution in original post

sdagley
Esteemed Contributor II

You might want to look at https://github.com/SecondSonConsulting/Renew which is a very configurable and polished tool for reminding users to restart their Macs. The Wiki link on that page covers installation and configuration. It does require SwiftDialog for user notifications so if that's an issue for your org the examples previously posted using Jamf Helper would work.

View solution in original post

Jan_H
New Contributor II
10 REPLIES 10

Anusan16
New Contributor

You can use extension attribute. Which will allow to identify the date when the device has been last restarted. Then use the extension attribute in a smart group.

#!/bin/bash

# get last reboot date and time
lastReboot=$( who -b )

# extract month, day and time
month=$( /usr/bin/awk '{ print $3 }' <<< "$lastReboot" )
day=$( /usr/bin/awk '{ print $4 }' <<< "$lastReboot" )
time=$( /usr/bin/awk '{ print $5 }' <<< "$lastReboot" )

# convert date and time to ISO date format
lastRebootISO=$( /bin/date -j -f "%b %e %l:%M" "$month $day $time" '+%Y-%m-%d %I:%M:00' )

echo "<result>$lastRebootISO</result>"

exit 0

Hope that helps.

DonCascone
New Contributor II

Thank you,
I already created the EA concerning the Last Reboot date and paired it to a SCG that contains every Macbook that is on for more then x days but I'm struggling to followup on this with the notification part of the issue.

Anusan16
New Contributor

Then you can use the Jamf Helper.  It will prompt a message on screen. Use this script on a policy then scope the SCG. Ensure you test to get the desired outcome.

#!/bin/bash

# Path to Jamf Helper
JAMF_HELPER="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"

# Message and options
HEADING="System Restart Required"
MESSAGE="To complete essential updates, please restart your device. You can choose to restart now or delay until later."

# Display the Jamf Helper window with two buttons
USER_CHOICE=$("$JAMF_HELPER" -windowType utility -title "$HEADING" -heading "$HEADING" -description "$MESSAGE" \
-icon "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns" \
-button1 "Restart Now" -button2 "Later" -defaultButton 1)

# Check the user's choice
if [ "$USER_CHOICE" == "0" ]; then
# Restart the device immediately
sudo shutdown -r now
else
# Postpone the restart and notify again in 1 hour (optional customization)
echo "User chose to restart later."
fi

angryant
New Contributor III

We couldnt find a way to do this either so we set out reboot policy to not run in business hours instead.

angryant_0-1730287800824.png

 

DonCascone
New Contributor II

Nice!
Seems like it's working I'll test a bit and I'll let you know.
Do you think it's possible to add something like a maximum number of times you could defer the policy? Something like 20 times before the message changes and you only have 1 button to restart the device.
Thank you again :)

you can try this with 5 deferrals and notification every1 hour 

#!/bin/bash

# Path to Jamf Helper
JAMF_HELPER="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"

# File to keep track of deferrals
DEFERRAL_FILE="/var/tmp/restart_deferral_count.txt"

# Message and options
HEADING="System Restart Required"
MESSAGE="To complete essential updates, please restart your device. You can choose to restart now or delay until later. After five delays, a restart will be required."

# Initialize deferral count if the file doesn't exist
if [ ! -f "$DEFERRAL_FILE" ]; then
echo 0 > "$DEFERRAL_FILE"
fi

# Read the current deferral count
DEFERRAL_COUNT=$(cat "$DEFERRAL_FILE")

# Check if deferrals have reached the limit
if [ "$DEFERRAL_COUNT" -ge 5 ]; then
# Restart the device immediately after the fifth deferral
"$JAMF_HELPER" -windowType utility -title "$HEADING" -heading "$HEADING" -description "Restarting now to complete essential updates." \
-icon "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns" -button1 "OK" -defaultButton 1
sudo shutdown -r now
exit 0
fi

# Display the Jamf Helper window with two buttons
USER_CHOICE=$("$JAMF_HELPER" -windowType utility -title "$HEADING" -heading "$HEADING" -description "$MESSAGE" \
-icon "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns" \
-button1 "Restart Now" -button2 "Later" -defaultButton 1)

# Check the user's choice
if [ "$USER_CHOICE" == "0" ]; then
# Restart the device immediately
sudo shutdown -r now
else
# Increment the deferral count and update the file
DEFERRAL_COUNT=$((DEFERRAL_COUNT + 1))
echo "$DEFERRAL_COUNT" > "$DEFERRAL_FILE"

# Notify user of remaining deferrals
REMAINING_DEFERRALS=$((5 - DEFERRAL_COUNT))
echo "User chose to defer restart. $REMAINING_DEFERRALS deferrals remaining."

# Sleep for 1 hour (or adjust this delay as needed)
sleep 3600

# Re-run the script to prompt again
"$0" # Calls the script again
fi

stutz
Contributor

@DonCascone Depends on how you want to notify your end users. 

swiftDialog:
I would use a policy, scope it to the SCG and build out a swiftDialog (https://github.com/swiftDialog/swiftDialog) script to inform users.  This provides a lot of custom options to display the notification in a professional way.  I believe you can also do a "macOS Notification" style alert as well.

If you don't want to deal with installing swiftDialog you can just use Jamf's built-in binaries to throw up a popup window and do the same thing as above minus the fancy window customizations.

Jamf Helper Window:

Screenshot 2024-10-30 at 7.54.24 AM.png

 

 

#!/bin/bash

# jamfHelper window
userChoice=$("/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper" -windowType "hud" -title "Reboot Notification" -heading "Please Reboot" -description "We noticied you haven't rebooted in awhile.  Please reboot to ensure your Mac is in proper working order." -icon "/System/Library/CoreServices/Diagnostics Reporter.app/Contents/Resources/AppIcon.icns" --iconSize 100 -button1 "REBOOT" -button2 "CLOSE" -defaultButton 0 -lockHUD -timeout 60 -countdown -countdownPrompt "" -alignCountdown center)

if [ "$userChoice" == "0" ]; then

	echo "User Clicked: Reboot or timer expired"
	shutdown -r now


else

	echo "User Clicked: Close"

fi

 

 

 


AppleScript Notification:

Screenshot 2024-10-30 at 7.53.23 AM.png

 

 

#!/bin/bash

/usr/bin/osascript <<APPLESCRIPT

display notification "You haven't rebooted in awhile.  Please do this at your earliest convenience." with title "Reboot Reminder"

APPLESCRIPT

 


As always run these scripts on a test machine before putting them into PROD.

sdagley
Esteemed Contributor II

You might want to look at https://github.com/SecondSonConsulting/Renew which is a very configurable and polished tool for reminding users to restart their Macs. The Wiki link on that page covers installation and configuration. It does require SwiftDialog for user notifications so if that's an issue for your org the examples previously posted using Jamf Helper would work.

Jan_H
New Contributor II

We use the support app for this: https://github.com/root3nl/SupportApp Screenshot Preview - 2024-10-30 at 13.56.41@2x.png

acodega
New Contributor

Renew on GitHub is great for this. It's MDM agnostic. It's also a good script to review and learn some things.