Posted By

pantuts on 07/25/14


Tagged

check python cron battery stat


Versions (?)

Python: check battery stat and run through cron


 / Published in: Python
 

URL: http://pantuts.com/2014/07/25/cron-job-python-script-check-battery-stat/

for Cron:

run

$ crontab -e

then add this

*/1 * * * * export DISPLAY=:0.0 && /home/username/path/to/script.py > /dev/null

then save. This cron job will run every minute
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Dependecy: notify2
  4. # sudo pip install notify2
  5.  
  6. import notify2
  7. import shlex
  8. from subprocess import Popen, PIPE
  9.  
  10. upower = shlex.split('upower -i /org/freedesktop/UPower/devices/battery_BAT0')
  11. grp = shlex.split('grep percentage')
  12. ak = shlex.split("awk -F '[^0-9]*' '$0=$2'")
  13.  
  14. proc1 = Popen(upower, stdout=PIPE)
  15. proc2 = Popen(grp, stdin=proc1.stdout, stdout=PIPE)
  16. proc3 = Popen(ak, stdin=proc2.stdout, stdout=PIPE)
  17. proc1.stdout.close()
  18. out = int(proc3.communicate()[0].decode('ascii').rstrip())
  19. # print(out)
  20.  
  21. notify2.init('bat_check')
  22.  
  23. # edit this
  24. # if battery is less than 15%
  25. if out < 15:
  26. note = notify2.Notification('Battery Check', 'CHARGE CHARGE CHARGE CHARGE CHARGE')
  27. note.set_category('device')
  28. note.set_timeout(5000)
  29. note.show()

Report this snippet  

You need to login to post a comment.