Last active
February 1, 2017 08:21
-
-
Save stsimb/7b7d011d19b4538d5a4481a10ea78c73 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # mac battery logger | |
| # stsimb jan 2017 | |
| # allow only one concurrent instance to run | |
| LOCKFILE="/tmp/${SCRIPT_NAME}.lock" | |
| [ -f ${LOCKFILE} ] && logger "$0 already running......" && echo "Already running..." && exit 1 | |
| date > "${LOCKFILE}" | |
| # vars for log function | |
| PROG="$(basename $0 .sh)" | |
| PID="$$" | |
| LOGFILE="$(dirname $0)/${PROG}.log" | |
| # log to screen and logfile | |
| log() { | |
| echo "$@" | |
| echo "$(date) ${HOSTNAME} ${PROG}[${PID}] $@" >> ${LOGFILE} | |
| } | |
| # tempfile | |
| TF="$(mktemp)" | |
| # cleanup trap | |
| cleanup() { | |
| #log "Cleanup.." | |
| /bin/rm -f "${TF}" | |
| /bin/rm -f "${LOCKFILE}" | |
| } | |
| trap cleanup EXIT | |
| # send output to tempfile | |
| /usr/sbin/system_profiler SPPowerDataType > ${TF} | |
| # populate variabled with variables we're interested in | |
| CHARGING="$(grep 'Charging:' ${TF} | head -1 | awk '{print $2}')" | |
| FULLY_CHARGED="$(grep 'Fully Charged:' ${TF} | awk '{print $3}')" | |
| FULL_CAPACITY="$(grep 'Full Charge Capacity' ${TF} | awk '{print $5}')" | |
| REMAINING="$(grep 'Charge Remaining' ${TF} | awk '{print $4}')" | |
| COUNT="$(grep 'Cycle Count:' ${TF} | awk '{print $3}')" | |
| CONNECTED="$(grep 'Connected:' ${TF} | awk '{print $2}')" | |
| PERC=$(expr $(expr 100 \* ${REMAINING}) / ${FULL_CAPACITY} ) | |
| TXT="Charger_Connected=${CONNECTED} Charging=${CHARGING} Full=${FULLY_CHARGED} Full_Capacity=${FULL_CAPACITY} Remaining_Capacity=${REMAINING} Percent_Full=${PERC} Cycle_Count=${COUNT}" | |
| log "${TXT}" | |
| # if fully charged, display a notification | |
| if [ "${CONNECTED}x" == "Yesx" ]; then | |
| if [ "${FULLY_CHARGED}x" == "Yesx" ]; then | |
| osascript -e "display notification \"Please disconnect the AC Charger.\" with title \"Battery Fully Charged\"" | |
| fi | |
| fi | |
| # if less than 5% remaining, display a notification | |
| if [ ${PERC} -le 5 ]; then | |
| if [ "${CONNECTED}x" == "Nox" ]; then | |
| osascript -e "display notification \"Please connect the AC Charger.\" with title \"Less than 5% battery remaining\"" | |
| fi | |
| fi | |
| # delete tempfile -- moved to cleanup trap | |
| #rm -f ${TF} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment