Skip to main content
Solved

Uptime script for EA does not output intended results

  • March 4, 2023
  • 4 replies
  • 14 views

Forum|alt.badge.img+3

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

Best answer by rqomsiya

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

4 replies

rqomsiya
Forum|alt.badge.img+12
  • Honored Contributor
  • 226 replies
  • Answer
  • March 4, 2023

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

AJPinto
Forum|alt.badge.img+26
  • Legendary Contributor
  • 2801 replies
  • March 6, 2023

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

 


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 8 replies
  • March 6, 2023

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

This worked well, thanks!


mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • 7886 replies
  • March 6, 2023

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.