Useful Scripts for Linux

Status
Not open for further replies.

KaptainBug

Level 12
Thread author
Verified
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.

Screenshot from 2014-06-09 00:17:25.png

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

  • Battery.txt
    1.3 KB · Views: 484
Last edited:
D

Deleted member 178

and how looks like the notification? big in the middle of the screen ?

i want it to alert me at 85% , because at 100% it will reduce your battery life, so i guess i change this line to 85%?

Code:
notify-send -i "$FULLBAT_ICON" "Battery is Full - 100%."


done the script as above, it uses 236kb of RAM :D

note than in some PCs , BAT0 must be renamed BAT1 or your battery will be not recognized
 
Last edited by a moderator:

KaptainBug

Level 12
Thread author
Verified
Dec 26, 2013
562
and how looks like the notification? big in the middle of the screen ?

i want it to alert me at 85% , because at 100% it will reduce your battery life, so i guess i change this line to 85%?

Code:
notify-send -i "$FULLBAT_ICON" "Battery is Full - 100%."


done the script as above, it uses 236kb of RAM :D

note than in some PCs , BAT0 must be renamed BAT1 or your battery will be not recognized
Pls find the script for 85%. I haven’t tested this. There should not be any problem, but if there is then pls let me know.

There wont be any notification in the center of the screen. Notification still gets displayed at the top right, but along with a sound. So even when you are working on full screen tasks like watching movies, you can recognize the battery status with the background sound.

Note that, if the battery is low at 40% then until you plug the charger to the outlet, it will notify and play a sound every 2 mins. Similarly when full charged, it will notify and play sound every 2 mins until you pull the plug.
 

Attachments

  • Battery85.txt
    1.6 KB · Views: 395
  • Like
Reactions: Deleted member 178

ZeroDay

Level 30
Verified
Top Poster
Well-known
Aug 17, 2013
1,905
Here's a simple script to help clean out your Ubuntu based distros. The script was created by: Ratick http://community.linuxmint.com/user/view/11791

The creators explanation of the script:

If you are an geeky user who keeps installing various software and updates then with the passage of time you will feel that your system is messed up. This is a shell script which lets you clean your system by removing the following:

  • apt cache
  • Config file for Uninstalled .deb packages
  • Unused Kernels
First lets discuss the different items which this script removes.

APT Cache

APT is the package management tool which is used to install/upgrade and remove packages in your Ubuntu system. If you use it often then it’s cache consumes a lot of space. By removing the apt cache you get plenty of free space.

Config Files For Uninstalled .Deb Packages

Suppose you downloaded a package for any software and it did not work. Many users forget to delete such packages. The Ubucleaner takes care of such packages and throws them out of the system.

Unused Kernel

There could be more than one kernel present in your system at the same time. But only one will be functional. This script gets rid of all unused kernels to free up system space and keep your system neat and clean.

The Script:

Code:
#!/bin/bash
OLDCONF=$(dpkg -l|grep "^rc"|awk '{print $2}')
CURKERNEL=$(uname -r|sed 's/-*[a-z]//g'|sed 's/-386//g')
LINUXPKG="linux-(image|headers|ubuntu-modules|restricted-modules)"
METALINUXPKG="linux-(image|headers|restricted-modules)-(generic|i386|server|common|rt|xen)"
OLDKERNELS=$(dpkg -l|awk '{print $2}'|grep -E $LINUXPKG |grep -vE $METALINUXPKG|grep -v $CURKERNEL)
YELLOW="\033[1;33m"
RED="\033[0;31m"
ENDCOLOR="\033[0m"
if [ $USER != root ]; then
  echo -e $RED"Error: must be root"
  echo -e $YELLOW"Exiting..."$ENDCOLOR
  exit 0
fi
echo -e $YELLOW"Cleaning apt cache..."$ENDCOLOR
aptitude clean
echo -e $YELLOW"Removing old config files..."$ENDCOLOR
sudo aptitude purge $OLDCONF
echo -e $YELLOW"Removing old kernels..."$ENDCOLOR
sudo aptitude purge $OLDKERNELS
echo -e $YELLOW"Emptying every trashes..."$ENDCOLOR
rm -rf /home/*/.local/share/Trash/*/** &> /dev/null
rm -rf /root/.local/share/Trash/*/** &> /dev/null
echo -e $YELLOW"Script Finished!"$ENDCOLOR

open text editor, copy-paste code above, named to mintcleaner.sh and save it to your home folder


Then, open terminal, type: sudo chmod +x mintcleaner.sh

Now, run the script with the following command and it will take care of all things that mentioned above


sudo sh mintcleaner.sh.

Source: http://community.linuxmint.com/tutorial/view/373
 

TeleGuy

New Member
Jan 19, 2016
4
About half-way down, in the mintcleaner.sh script, is a line that contains "fi". Is that correct, or should it be "if"?

Thanks, guys.
 
  • Like
Reactions: Rishi

TeleGuy

New Member
Jan 19, 2016
4
Thanks for your very edifying reply, Klipsh! Now I know.

Follow-up:

Is mintcleaner.sh the most efficient (and safest) way to "clean" a Linux OS?

Currently, I run sudo apt-get: Update, Clean, Autoclean, Remove, then I run the Debororphan gui to remove orphan files. Does this process also remove old kernels?

I want a lean, mean fighting Linux machine, but I'm treading very lightly due to my lack of expertise in the Linux environment.

Again, thanks for your help!
 
  • Like
Reactions: LabZero and Myriad

KaptainBug

Level 12
Thread author
Verified
Dec 26, 2013
562
You don't need to clean junk in linux, unless you have low disk space in your HDD.. Cleaning junk in linux wont improve system performance like windows. So if you have plenty of hard disk space don't bother cleaning junk/cache.
 

thahin

New Member
Aug 24, 2016
1
can you help me? I want to sound alert when unplugged and plugged ac power,Please
 

Myriad

Level 7
Verified
Well-known
May 22, 2016
349
@KaptainBug @ZeroDay

Thank you for sharing the scripts .... very much appreciated !

Also , great idea for starting a thread to share scripts :) ;)

..... long may it run !

Thanks for your very edifying reply, Klipsh! .....
- and -
I want a lean, mean fighting Linux machine,
Again, thanks for your help!

..... my edits above ^^

@TeleGuy @Klipsh

Well said amigo ! ..... that goes for me too !

I'm treading very lightly due to my lack of expertise in the Linux environment.[/I]

That's the way to go ..... you are already on the right road to Linux wisdom !!


 
  • Like
Reactions: ZeroDay and LabZero
Status
Not open for further replies.

About us

  • MalwareTips is a community-driven platform providing the latest information and resources on malware and cyber threats. Our team of experienced professionals and passionate volunteers work to keep the internet safe and secure. We provide accurate, up-to-date information and strive to build a strong and supportive community dedicated to cybersecurity.

User Menu

Follow us

Follow us on Facebook or Twitter to know first about the latest cybersecurity incidents and malware threats.

Top