Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. stsimb@koala3:~$ .bin/batterylog.sh
  2. Charger_Connected=Yes Charging=No Full=Yes Full_Capacity=6216 Remaining_Capacity=6216 Cycle_Count=777
  3.  
  4. stsimb@koala3:~$ .bin/batterylog.sh
  5. Charger_Connected=No Charging=No Full=Yes Full_Capacity=6216 Remaining_Capacity=6216 Cycle_Count=777
  6.  
  7. stsimb@koala3:~$ crontab -l
  8. * * * * * ~/.bin/batterylog.sh
  9.  
  10. stsimb@koala3:~$ cat .bin/batterylog.sh
  11. #!/bin/bash
  12. # mac battery logger
  13. # stsimb jan 2017
  14.  
  15. # allow only one concurrent instance to run
  16. LOCKFILE="/tmp/${SCRIPT_NAME}.lock"
  17. [ -f ${LOCKFILE} ] && logger "$0 already running......" && echo "Already running..." && exit 1
  18. date > "${LOCKFILE}"
  19.  
  20. # vars for log function
  21. PROG="$(basename $0 .sh)"
  22. PID="$$"
  23. LOGFILE="$(dirname $0)/${PROG}.log"
  24.  
  25. # log to screen and logfile
  26. log() {
  27. echo "$@"
  28. echo "$(date) ${HOSTNAME} ${PROG}[${PID}] $@" >> ${LOGFILE}
  29. }
  30.  
  31. # tempfile
  32. TF="$(mktemp)"
  33.  
  34. # cleanup trap
  35. cleanup() {
  36. #log "Cleanup.."
  37. /bin/rm -f "${TF}"
  38. /bin/rm -f "${LOCKFILE}"
  39. }
  40. trap cleanup EXIT
  41.  
  42. # send output to tempfile
  43. /usr/sbin/system_profiler SPPowerDataType > ${TF}
  44.  
  45. # populate variabled with variables we're interested in
  46. CHARGING="$(grep 'Charging:' ${TF} | head -1 | awk '{print $2}')"
  47. FULLY_CHARGED="$(grep 'Fully Charged:' ${TF} | awk '{print $3}')"
  48. FULL_CAPACITY="$(grep 'Full Charge Capacity' ${TF} | awk '{print $5}')"
  49. REMAINING="$(grep 'Charge Remaining' ${TF} | awk '{print $4}')"
  50. COUNT="$(grep 'Cycle Count:' ${TF} | awk '{print $3}')"
  51. CONNECTED="$(grep 'Connected:' ${TF} | awk '{print $2}')"
  52.  
  53. TXT="Charger_Connected=${CONNECTED} Charging=${CHARGING} Full=${FULLY_CHARGED} Full_Capacity=${FULL_CAPACITY} Remaining_Capacity=${REMAINING} Cycle_Count=${COUNT}"
  54.  
  55. log "${TXT}"
  56.  
  57. # if fully charged, display a notification
  58. if [ "${CONNECTED}x" == "Yesx" ]; then
  59. if [ "${FULLY_CHARGED}x" == "Yesx" ]; then
  60. osascript -e "display notification \"Please disconnect the AC Charger.\" with title \"Battery Fully Charged\""
  61. fi
  62. fi
  63.  
  64. # delete tempfile -- moved to cleanup trap
  65. #rm -f ${TF}
  66. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement