Uptime script for EA does not output intended results

jagstang94
New Contributor II

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>"

1 ACCEPTED SOLUTION

rqomsiya
Contributor III

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

View solution in original post

4 REPLIES 4

rqomsiya
Contributor III

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

jagstang94
New Contributor II

This worked well, thanks!

AJPinto
Honored Contributor II

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

 

mm2270
Legendary Contributor III

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.