Useful Scripts for Linux

Discussion in 'Linux' started by KaptainBug, Jun 9, 2014.

  1. KaptainBug

    KaptainBug Member

    Creating this thread to share useful scripts for Linux.



    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.

    [​IMG]

    --------------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
    ---------------
     

    Attached Files:

  2. Google Adsense

  3. Bala

    Bala Administrator Staff Member

    Thanks captain bug, but the Mate desktop environment already has an applet for this I suppose but never the less it is a good addition for Cinnamon users.
     
  4. illumination

    illumination Guest

    It is good all the way around, because things that function properly on your system, may not on others.. Posting found work arounds and solutions will be a great addition to any Linux section.. :)
     

Share This Page