Uptime User Notification for 10.9+

msimpson
New Contributor

Thought this might be helpful for someone else. Gives an non-intrusive little Notification Centre notification to users that have an uptime for longer than x days. Doesn't rely on extension attributes or recon data that could be slightly out of date and not reflect reality.
Leaves a little breadcrumb of uptime in logs for posterity.

#!/bin/sh
##
##
##  Purpose: Notifies user if uptime is 2+ days
##  Details: Uses Notification Centre, notification (by default) disappears
##           after a few seconds. Doesn't rely on potentially out-of-date
##           recon/attribute data. No notification if uptime /right now/ is 
##           less than $minimum_days days.
##  Limitations: 10.9+ only.
##  
##  matt.simpson @at@ me.com ##
##

minimum_days=3 ## single digit integer from 1 -> 9

uptime=$(uptime | egrep "([$minimum_days-9] day|dd day)" | awk '{print $3 " "$4}' | sed 's/,//')
if [ -n "$uptime" ] 
then
    echo "Uptime: $uptime"
    osascript -e 'display notification "" with title "Computer not shut down for '"$uptime"'" subtitle "Consider restarting soon for best performance"'
else
    echo "Uptime: < $minimum_days days"
fi

Could be used in conjunction with an extension attribute like this:

#!/bin/sh
uptime=$(uptime | egrep "(d day)" | awk '{print $3}')
if [ "$uptime" == "" ] ; then
    echo "<result>0</result>"
else
    echo "<result>$uptime</result>"
fi

Cheers,
Matt

0 REPLIES 0