Posted on 03-03-2023 05:23 PM
Hi All,
I'm trying to use this script to determine if a Mac's uptime is 5 days or longer, then output results as "Yes" if so, and "No" if not. But when configured as an EA, it only gives the "No" result. What am I missing?
#!/bin/bash
# Get the system uptime in seconds
uptime_seconds=$(awk '{print $1}' /proc/uptime)
# Calculate the uptime in days
uptime_days=$(echo "$uptime_seconds/86400" | bc)
# Check if the uptime is greater than or equal to 5 days
if [ $uptime_days -ge 5 ]; then
result="Yes"
else
result="No"
fi
# Output the result in the correct format for Jamf Pro
echo "<result>$result</result>"
Solved! Go to Solution.
Posted on 03-03-2023 06:57 PM
@jagstang94 : You can try something like this to calculate your uptime in seconds:
#!/bin/bash
# Get the system uptime in seconds
uptime_seconds=$(uptime | awk '{print $3}' | awk -F: '{print ($1 * 86400) + ($2 * 3600) + ($3 * 60) + $4}')
# Calculate the uptime in days
uptime_days=$(echo "$uptime_seconds/86400" | bc)
# Check if the uptime is greater than or equal to 5 days
if [ $uptime_days -ge 5 ]; then
result="Yes"
else
result="No"
fi
# Output the result in the correct format for Jamf Pro
echo "<result>$result</result>"
Posted on 03-03-2023 06:57 PM
@jagstang94 : You can try something like this to calculate your uptime in seconds:
#!/bin/bash
# Get the system uptime in seconds
uptime_seconds=$(uptime | awk '{print $3}' | awk -F: '{print ($1 * 86400) + ($2 * 3600) + ($3 * 60) + $4}')
# Calculate the uptime in days
uptime_days=$(echo "$uptime_seconds/86400" | bc)
# Check if the uptime is greater than or equal to 5 days
if [ $uptime_days -ge 5 ]; then
result="Yes"
else
result="No"
fi
# Output the result in the correct format for Jamf Pro
echo "<result>$result</result>"
Posted on 03-06-2023 08:30 AM
This worked well, thanks!
Posted on 03-06-2023 06:56 AM
This is what I use. I do the day count with a smart group, so I can use one EA for multiple groups.
#!/bin/sh
dayCount=$( uptime | awk -F "(up | days)" '{ print $2 }' )
if ! [ "$dayCount" -eq "$dayCount" ] 2> /dev/null ; then
dayCount="0"
fi
echo "<result>$dayCount</result>"
exit 0
Posted on 03-06-2023 12:44 PM
My EA gets the system boot time, converts it into a YYYY-MM-DD HH:MM:SS format, which I can then use in Jamf to build a Smart Computer Group like
Last Boot Time | more than x days ago | 15
which gives me all devices that have been up for more than 15 days.