- Dec 26, 2013
- 562
Creating this thread to share useful scripts for Linux.
Script #1: To monitor battery and display notification and play a sound
Script #2: Simple Script to Keep Up Your System Clean
Script #1: To monitor battery and display notification and play a sound
Linux distro already have low battery notification, but when watching movies in youtube or doing other tasks in full screen it does not display a notification. I got annoyed when I did not get any notification and the laptop shut down itself when it reached critical battery. So I wrote this script.
How to use: Download the attached file and rename it to Battery.sh
Right click the file and click properties. Select permissions tab and check 'Allow executing the file as program'
Open startup applications and add the script to startup. Restart the machine and you are done. Now you will get a pop-up notification and sound when battery is low and when charging is complete. Also when the battery reaches 5%, system will shut down.
This script is written for Linux Mint 17. So all the icons, battery path and sound will work fine in Linux Mint 17. You can change few parameters as per your distro and use this script.
Let me know if you have any suggestions to improve the script.
--------------Battery.sh-------------
#!/bin/bash
# low battery in %
LOW_BATTERY="40"
# critical battery in % (execute action)
CRITICAL_BATTERY="5"
# action
ACTION="/sbin/poweroff"
# sleep 5 mins
SLEEP="300"
# display icon
LOWBAT_ICON="/usr/share/icons/gnome/32x32/status/battery-low.png"
CRITBAT_ICON="/usr/share/icons/gnome/32x32/status/battery-caution.png"
FULLBAT_ICON="/usr/share/icons/gnome/32x32/status/battery-full-charged.png"
# path to battery /sys
BATTERY_PATH="/sys/class/power_supply/BAT0/"
# notify sound
PLAY="aplay /usr/share/sounds/linuxmint-gdm.wav"
while [ true ]; do
if [ -e "$BATTERY_PATH" ]; then
BATTERY_ON=$(cat $BATTERY_PATH/status)
CURRENT_BATTERY=$(cat $BATTERY_PATH/capacity)
if [ "$BATTERY_ON" == "Discharging" ]; then
if [ "$CURRENT_BATTERY" -lt "$CRITICAL_BATTERY" ]; then
$($PLAY)
notify-send -i "$CRITBAT_ICON" "Battery is Critical - $CURRENT_BATTERY%. Shutting down in 60 sec."
sleep 60
$($ACTION)
elif [ "$CURRENT_BATTERY" -lt "$LOW_BATTERY" ]; then
$($PLAY)
notify-send -i "$LOWBAT_ICON" "Battery is Low - $CURRENT_BATTERY%."
fi
elif [ "$BATTERY_ON" == "Full" ]; then
$($PLAY)
notify-send -i "$FULLBAT_ICON" "Battery is Full - 100%."
fi
fi
sleep $SLEEP
done
---------------
Script #1: To monitor battery and display notification and play a sound
Script #2: Simple Script to Keep Up Your System Clean
Script #1: To monitor battery and display notification and play a sound
Linux distro already have low battery notification, but when watching movies in youtube or doing other tasks in full screen it does not display a notification. I got annoyed when I did not get any notification and the laptop shut down itself when it reached critical battery. So I wrote this script.
How to use: Download the attached file and rename it to Battery.sh
Right click the file and click properties. Select permissions tab and check 'Allow executing the file as program'
Open startup applications and add the script to startup. Restart the machine and you are done. Now you will get a pop-up notification and sound when battery is low and when charging is complete. Also when the battery reaches 5%, system will shut down.
This script is written for Linux Mint 17. So all the icons, battery path and sound will work fine in Linux Mint 17. You can change few parameters as per your distro and use this script.
Let me know if you have any suggestions to improve the script.
--------------Battery.sh-------------
#!/bin/bash
# low battery in %
LOW_BATTERY="40"
# critical battery in % (execute action)
CRITICAL_BATTERY="5"
# action
ACTION="/sbin/poweroff"
# sleep 5 mins
SLEEP="300"
# display icon
LOWBAT_ICON="/usr/share/icons/gnome/32x32/status/battery-low.png"
CRITBAT_ICON="/usr/share/icons/gnome/32x32/status/battery-caution.png"
FULLBAT_ICON="/usr/share/icons/gnome/32x32/status/battery-full-charged.png"
# path to battery /sys
BATTERY_PATH="/sys/class/power_supply/BAT0/"
# notify sound
PLAY="aplay /usr/share/sounds/linuxmint-gdm.wav"
while [ true ]; do
if [ -e "$BATTERY_PATH" ]; then
BATTERY_ON=$(cat $BATTERY_PATH/status)
CURRENT_BATTERY=$(cat $BATTERY_PATH/capacity)
if [ "$BATTERY_ON" == "Discharging" ]; then
if [ "$CURRENT_BATTERY" -lt "$CRITICAL_BATTERY" ]; then
$($PLAY)
notify-send -i "$CRITBAT_ICON" "Battery is Critical - $CURRENT_BATTERY%. Shutting down in 60 sec."
sleep 60
$($ACTION)
elif [ "$CURRENT_BATTERY" -lt "$LOW_BATTERY" ]; then
$($PLAY)
notify-send -i "$LOWBAT_ICON" "Battery is Low - $CURRENT_BATTERY%."
fi
elif [ "$BATTERY_ON" == "Full" ]; then
$($PLAY)
notify-send -i "$FULLBAT_ICON" "Battery is Full - 100%."
fi
fi
sleep $SLEEP
done
---------------
Attachments
Last edited: