Last active
August 29, 2015 14:19
-
-
Save onilton/ed73d84e0f32a6eb3213 to your computer and use it in GitHub Desktop.
suspend-on-low-battery
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 | |
# This is a fix for my samsung np900x4c laptop, who doesn't seem to shutdown nicely when low on battery | |
# Just put this in your cron (Runs every 1 minute). | |
# * * * * * /home/YOURUSERNAME/bin/suspend-on-low-battery | |
# ==== START FIX ==== | |
# Command notify-send would not show the message on your | |
# screen when started by cron. | |
# This fix will work in any crontab. sudo crontab-e or crontab -e | |
# Make notify-send know where to send msgs http://askubuntu.com/a/472769/154623 | |
export DISPLAY=:0 | |
# Gets current loggedin GNOME's username http://askubuntu.com/a/324746/154623 | |
GNOME_USER=`ps -e -o user:30,pid,ppid,c,stime,tty,time,cmd | grep gnome-session | grep -v grep | head -1 | cut -d ' ' -f1` | |
# http://askubuntu.com/a/298622/154623 | |
notify_send="sudo -u $GNOME_USER /usr/bin/notify-send" | |
# ===== END FIX ==== | |
battery_device=`sudo upower -e | grep -i battery | head -n 1` | |
battery_percentage=`sudo upower -i $battery_device | grep percentage | head -n 1 | cut -d ':' -f 2 | tr -d ' ' | tr -d '%'` | |
battery_state=`sudo upower -i $battery_device | grep state | head -n 1 | cut -d ':' -f 2 | tr -d ' '` | |
[ 20 -le $battery_percentage -a $battery_percentage -lt 25 -a $battery_state == "discharging" ] && $notify_send "A bateria está abaixo de 25%, vou desligar quando chegar em 10%" | |
[ 17 -le $battery_percentage -a $battery_percentage -lt 20 -a $battery_state == "discharging" ] && $notify_send "A bateria está abaixo de 20%, vou desligar quando chegar em 15%" | |
[ 16 -le $battery_percentage -a $battery_percentage -lt 17 -a $battery_state == "discharging" ] && $notify_send "A bateria está em 16%, VOU DESLIGAR daqui a SEGUNDOS, quando chegar em 15%" | |
# Gnome friendly suspend http://askubuntu.com/a/131022/154623 | |
[ $battery_percentage -le 15 -a $battery_state == "discharging" ] && $notify_send "Bateria em 15%, SUSPENDENDO AGORA!" && sleep 5 && \ | |
dbus-send --system --print-reply \ | |
--dest="org.freedesktop.UPower" \ | |
/org/freedesktop/UPower \ | |
org.freedesktop.UPower.Suspend | |
# Uncomment to to make sure notify-send is really working | |
# $notify_send "Rodou!!!" >> /home/oniltonmaciel/bin/suspend.log 2>&1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment