Restart Computer After X Days of Uptime

lvschnider
New Contributor II

I am trying to find a way to create a policy that will restart a computer after it's been up for X number of days. I don't want it to force a reboot instantly though. I want the user to have the option to postpone it 2 or 3 times until it forces. Has anyone had any luck with this? 

2 ACCEPTED SOLUTIONS

john_sherrod
Contributor II

We did something similar, though ours just requested that the user restart. Basically I created a smart group called "Report - Uptime Is Greater Than 21 Days" where the criteria is "Days Since Last Restart" is more than 21 days. I then created a policy scoped to that group that just uses the "User Interaction" tab of the policy to send a message that shows up as a notification on their screen encouraging them to restart. We've seen great success in keeping the number of users who haven't restarted in more than 21 days down since then.

View solution in original post

robjschroeder
Contributor

Hey all! I've used this EA in the past, it will format the last reboot time in a way that you can set the Data Type on the EA to 'Date (YYYY-MM-DD hh:mm:ss)'. Then with your Smart Computer Group, you'll be able to use 'more than x days ago' operator. Here is the script:

#!/bin/bash

# Get boottime in epoch time, convert to Jamf Pro formatted time and make an extension attribute
#
# Updated: 2.23.2022 @ Robjschroeder
#

bootTime=$(sysctl kern.boottime | awk '{print $5}' | tr -d ,)
formattedTime=$(date -jf %s $bootTime "+%F %T")
echo "<result>${formattedTime}</result>"

exit 0

View solution in original post

30 REPLIES 30

john_sherrod
Contributor II

We did something similar, though ours just requested that the user restart. Basically I created a smart group called "Report - Uptime Is Greater Than 21 Days" where the criteria is "Days Since Last Restart" is more than 21 days. I then created a policy scoped to that group that just uses the "User Interaction" tab of the policy to send a message that shows up as a notification on their screen encouraging them to restart. We've seen great success in keeping the number of users who haven't restarted in more than 21 days down since then.

I think that sounds great! How did you use that criteria for the smart group? I don't see anything that references reboot or or last restart. Is that a custom extension attribute? 

scottb
Honored Contributor

Here's a time since last reboot XATTR that I use...

#!/bin/bash

## last system reboot

btt=`sysctl kern.boottime | awk -F'} ' '{print $NF}' | awk '{print $NF,$2,$3,$4}'`

echo "<result>$btt</result>"

Good question! Yes, here's the extension attribute I came up with:

#!/bin/sh

uptime=$( uptime )
dayCount=$( uptime | awk -F "(up | days)" '{ print $2 }' | sed 's/[ day].*//' )

if [[ $uptime == *"day"* ]]; then
    echo "<result>$dayCount</result>"
    
else echo "<result>0</result>"
    
fi
    
exit 0

 

scottb
Honored Contributor

^ that's better! 👍

That one took some trial and error for sure. 😂

scottb
Honored Contributor

Since I can't reboot anyone's Mac, I made one to just show date of last reboot, but for this situation, yours is much more useful.

lvschnider
New Contributor II

This is exactly what I wanted! Thank you so much for your help. I will created that policy and see if it helps. 

Awesome! Very happy I could be of service!

Thank you for this!

aandino
New Contributor III

Thanks for sharing this. I'm working on implementing this myself, but I'm not quite clear on where the "...criteria is "Days Since Last Restart" is more than 21 days"

How does this look in the criteria section? Specifically the operator and value specified.

Thank you!

Good question! "Days Since Last Restart" is the name of my extension attribute, so I can add it by choosing "Show Advanced Criteria" when adding criteria to my smart group. My extension attribute Data Type is set to integer, which gives me the "More Than" operator in the smart group, and I typed in "21" in the Value field.

aandino
New Contributor III

Ah ha, of course, I had selected text instead of integer, that would be the problem. Thank you!

You're very welcome!

robjschroeder
Contributor

Hey all! I've used this EA in the past, it will format the last reboot time in a way that you can set the Data Type on the EA to 'Date (YYYY-MM-DD hh:mm:ss)'. Then with your Smart Computer Group, you'll be able to use 'more than x days ago' operator. Here is the script:

#!/bin/bash

# Get boottime in epoch time, convert to Jamf Pro formatted time and make an extension attribute
#
# Updated: 2.23.2022 @ Robjschroeder
#

bootTime=$(sysctl kern.boottime | awk '{print $5}' | tr -d ,)
formattedTime=$(date -jf %s $bootTime "+%F %T")
echo "<result>${formattedTime}</result>"

exit 0

I am pretty new to this, and I added the EA and then i create the smart computer group, when i view the results is there a way to add the uptime filed in the results to make sure the data is correct? I currently have last Check-in showing.

 

m_oravec
New Contributor II

Got it working, I was impatient and expected immediate reports,.

 

egusman
New Contributor II

The script and settings work but really not practical at all. During the first half of the day, it only detected 5 Mac's. With this rate it would take a whole week to extract a report.

That's the joy of using an EA, your computers need to submit inventory to get the data

EliasG
Contributor

I have the script running and now I can see last restarts.

 

How do I create something to where if a user hasn't restarted in x amounts of days they need to reboot/shut down laptop warning? 

I created a policy called "Time To Restart."

Its trigger is recurring checkin, and its frequency is once per day.

It's scoped to my "Report - Uptime Is Greater Than 21 Days" smart group.

All the policy does is leverage the User Interaction tab to display the following message as a notification:

"Your Mac hasn't been restarted in more than 21 days. Restarting your Mac at least once a week helps keep it running optimally. - IT"

I wanted to keep the wording short so it all fit within the small notification banner.

I can't get the smart group to see anything, I'm missing some settings or config. Screen Shot 2023-10-20 at 11.45.21 AM.png

Here's what my smart group criteria looks like. I've got my operator set to "more than."Screenshot 2023-10-20 at 10.48.14 AM.png

Thank you I appreciate it! Can I bug you for one more thing? What does your script look like? 

 

Thanks. 

Sure! Here's mine:

#!/bin/sh

uptime=$( uptime )
dayCount=$( uptime | awk -F "(up | days)" '{ print $2 }' | sed 's/[ day].*//' )

if [[ $uptime == *"day"* ]]; then
    echo "<result>$dayCount</result>"
    
else echo "<result>0</result>"
    
fi
    
exit 0

 

Good Morning John

I meant this policy for this

All the policy does is leverage the User Interaction tab to display the following message as a notification:

"Your Mac hasn't been restarted in more than 21 days. Restarting your Mac at least once a week helps keep it running optimally. - IT"

Have you checked out Renew? https://github.com/SecondSonConsulting/Renew

Screenshot 2023-10-23 at 9.52.28 AM.png

m_oravec
New Contributor II

Looks exactly like mine, and my policy and smart groups.

 

egusman
New Contributor II

The biggest issue is that users now move the popup window to the edge of the screen to ignore it and therefore the script / policy will never be fully executed.