Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- stsimb@koala3:~$ .bin/batterylog.sh
- Charger_Connected=Yes Charging=No Full=Yes Full_Capacity=6216 Remaining_Capacity=6216 Cycle_Count=777
- stsimb@koala3:~$ .bin/batterylog.sh
- Charger_Connected=No Charging=No Full=Yes Full_Capacity=6216 Remaining_Capacity=6216 Cycle_Count=777
- stsimb@koala3:~$ crontab -l
- * * * * * ~/.bin/batterylog.sh
- stsimb@koala3:~$ cat .bin/batterylog.sh
- #!/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}')"
- TXT="Charger_Connected=${CONNECTED} Charging=${CHARGING} Full=${FULLY_CHARGED} Full_Capacity=${FULL_CAPACITY} Remaining_Capacity=${REMAINING} 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
- # delete tempfile -- moved to cleanup trap
- #rm -f ${TF}
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement