Skip to content

Instantly share code, notes, and snippets.

@iisti
Last active January 8, 2020 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iisti/25fd3504dc2fcd50bb639ae406328200 to your computer and use it in GitHub Desktop.
Save iisti/25fd3504dc2fcd50bb639ae406328200 to your computer and use it in GitHub Desktop.
Simple script for checking time interval when MacOS' display has turned on and off. This is intended to be used for tracking work hours.
#!/usr/bin/env bash
# A script for checking when MacOS has been in use.
# This script is meant for tracking work hours.
# Script checks when display has been turned on and off and calculates time difference.
# Result is given in format HH:MM:SS, that's the time difference between first display on
# and last display off log messages.
#
# The script is using only one log file (one day) at a time, so working overnight and then checking
# hours won't probably give meaningful result.
# Show parameter (which log file has been read) or instructions how to run the script.
if [ "$1" != "" ]; then
echo "Parameter is:"
echo $1
else
echo "Parameter is empty. Run the script by:"
echo " script_name logfile"
echo "For example:"
echo " $0 /var/log/powermanagement/2019.10.16.asl all"
exit 1
fi
# If parameter all has been given, print all display on/off entries.
if [ "$2" = "all" ]; then
echo "Parameter all was given, printing all display on/off log entries:"
syslog -f $1 2>/dev/null | grep -i display
fi
# 2>/dev/null is for supperssing "NOTE: Most system logs have moved to a new logging system. See log(1) for more information."
time_stamps=$(syslog -f $1 2>/dev/null | grep -i display | sed -e 1b -e '$!d')
# If no display on/off time stamps has been found from the log file, print "no work",
# else calculate working hours.
if [ -z "$time_stamps" ]
then
echo "No work done on $1"
else
echo "Time stamps when laptop display has been turned on and off:"
syslog -f $1 2>/dev/null | grep -i display | sed -e 1b -e '$!d'
start_secs=$(date -j -f '%H:%M:%S' $(syslog -f $1 2>/dev/null | grep -i display | head -1 | tr -s ' ' | cut -d' ' -f 3) '+%s')
stop_secs=$(date -j -f '%H:%M:%S' $(syslog -f $1 2>/dev/null | grep -i display | tail -1 | tr -s ' ' | cut -d' ' -f 3) '+%s')
working_hours=$(($stop_secs-$start_secs))
date -u -r $working_hours '+%H:%M:%S'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment