sandeepk

automation

I have written this script a few months back, that runs the application that I use daily for work, If I login into the system at a specific time interval :)

In this logic we check what is the day of the week is, If it is Saturday or Sunday exit the script. The command used here is – cut – remove sections from each line of files – d – use DELIM instead of TAB for the field delimiter – f select only these fields; also print any line that contains no delimiter character, unless the -s option is specified*

day=$(date | cut -d " " -f1)
not_days=(Sun Sat)
if [[ " ${not_days[@]} " =~ $day ]]
then
    exit 1
fi

Here we're calculating the difference with the current time on the system, If the difference is less than one, we will specify the application we want to run, else exit.

dbtimestamp=1050
secondsDiff=$(( `date '+%H%M'` - $dbtimestamp ))
echo $secondsDiff

So here is the full script

#!/bin/bash                                                               

day=$(date | cut -d " " -f1)
not_days=(Sun Sat)
if [[ " ${not_days[@]} " =~ $day ]]
then
    exit 1
fi

dbtimestamp=1050
secondsDiff=$(( `date '+%H%M'` - $dbtimestamp ))

if [ $secondsDiff -lt 1 ]
then
  hexchat &
  firefox &
  safeeyes &
 # other application you want to open
else
  exit 0
fi ;

There are some improvements which I will do in the scripts in future – Time delta logic for running the application needs to be improved. – Flag to override the all check condition and run the applications

Cheers!

#100DaysToOffload #Automation #Bash

While working from home one of the issue I faced that my laptop battery charger adapter is always remain plugged in almost all the time, due to which I have to replace my laptop battery. To deal with the problem I have now written a script to notify me about the laptop battery charging level if it goes above 85 % and below 20%.

#! /bin/bash                                                                                                                                          

while true
do

    battery_level=`acpi -b | grep -o '[0-9]*' | sed -n  2p`
    ac_power=`cat /sys/class/power_supply/AC/online`

    #If above command raise an error " No such file or directory" try the below command.
    #ac_power=`cat /sys/class/power_supply/ACAD/online`
    if [ $ac_power -gt 0 ]; then
        if [ $battery_level -ge 85 ]; then
            notify-send "Battery Full" "Level: ${battery_level}%"
        fi
    else
        if [ $battery_level -le 20 ]; then
            notify-send --urgency=CRITICAL "Battery Low" "Level: ${battery_level}%"
        fi
    fi
    sleep 120

done

The important commands which I want to break down to explain actually what they are doing.

  • First is acpi which tell us about the battery information and other ACPI information
  • grep command is used to extract the integer value from the acpi command
  • sed is used to get the second value from the grep result.
>> acpi -b
Battery 0: Discharging, 54%, 02:03:37 remaining
>>acpi -b | grep -o '[0-9]*'
0
54
02
04
41
>>acpi -b | grep -o '[0-9]*' | sed -n  2p
54
  • After that, we check that the charger is plugged in or not, based on that we check that the battery level does not exceed the described limit, if so is the case send the notification.
  • Then we check for the battery low indication which sends the notification if the battery level less than 20 %.
  • These condition is put in a continuous loop to check after a sleep time of 120s.

To make this script run automatically you have to assign the execution permission and specifies the execution command in the ~/.profile and reboot the system.

>> sudo chmod +x /path/to/battery-notification.sh

you can find my notes on shell commands here

thanks shrini for pointing out issue for Ubuntu 20 in lineac_power=`cat /sys/class/power_supply/AC/online` :) Cheers!

#100DaysToOffload #automation #scripts