check_shutdown.timer:
$ cat /etc/systemd/system/check_shutdown.timer [Unit] Description=Check if battery is low every 10 minutes [Timer] OnCalendar=*:0/10 Persistent=true [Install] WantedBy=timers.target
check_shutdown.service:
$ cat /etc/systemd/system/check_shutdown.service [Service] ExecStart= ExecStart=/home/jiajun/.xmonad/scripts/shutdown.py
check_shutdown.py:
#!/home/jiajun/.py3k/bin/python import psutil import logging import os import datetime bat = psutil.sensors_battery() logging.warn("%s: battery status: %s", datetime.datetime.now(), bat) if bat.percent < 15: logging.warn("gonna shutdown") os.system("sudo shutdown -h now")