Add battery script

This commit is contained in:
Andros Fenollosa 2023-05-02 15:36:38 +02:00
parent d77d05afb9
commit 1fa07b45c0
2 changed files with 238 additions and 0 deletions

121
README.md
View File

@ -3,6 +3,7 @@
Collection of Bash scripts to execute functionalities in folders, such as optimizing videos, unzipping files, converting images, etc.
- [Video optimizer](#video-optimizer)
- [Battery hook](#battery-hook)
- [Decompress files](#decompress-files)
---
@ -102,6 +103,126 @@ Add to document.
---
## Battery hook
Folder with scripts to be launched in different battery states.
The filename of the scripts must be:
- `discharging`: When the battery is in use.
- `charging`: When the battery is charging.
- `low`: When it reaches the low percentage. Default 15.
- `high`: When it reaches the high percentage. Default 85.
- `full`: When the battery is full.
They must have **execution permissions**. If any of them do not exist, they will be ignored.
### Install
``` sh
curl -o bash-folders-battery-hook https://raw.githubusercontent.com/tanrax/bash-folders/main/bash-folders-battery-hook.sh && chmod +x bash-folders-battery-hook && sudo mv bash-folders-battery-hook /usr/local/bin && echo "🎉 Successfully installed! 🎉"
```
Test
``` sh
bash-folders-battery-hook --help
```
### Run
``` sh
bash-folders-battery-hook --folder [folder path]
```
Example.
``` sh
mkdir battery-scripts
touch battery-scripts/discharging
chmod +x battery-scripts/discharging
touch battery-scripts/charging
chmod +x battery-scripts/charging
touch battery-scripts/low
chmod +x battery-scripts/low
touch battery-scripts/high
chmod +x battery-scripts/high
touch battery-scripts/full
chmod +x battery-scripts/full
bash-folders-battery-hook --folder battery-scripts
```
### Start at operating system startup
#### Option 1: Service
Create a file in `/etc/systemd/system/bash-folders-battery-hook.service` with the following content.
```ini
[Unit]
Description=Folder with scripts to be launched in different battery states.
[Service]
Restart=always
RestartSec=5
User=[user]
ExecStart=bash-folders-battery-hook --folder [folder path]
[Install]
WantedBy=multi-user.target
```
Edit it to your needs.
Now you will need the script to run every so often to check the battery status. The best solution is to create a `timer`.
Create a file in `/etc/systemd/system/bash-folders-battery-hook.timer` with the following content.
```ini
[Unit]
Description=Folder with scripts to be launched in different battery states every minute.
[Timer]
OnCalendar=*-*-* *:*:00
Persistent=true
[Install]
WantedBy=timers.target
```
Recharge services.
``` sh
sudo systemctl daemon-reload
```
And activate it.
``` sh
sudo systemctl enable bash-folders-battery-hook.timer
sudo systemctl start bash-folders-battery-hook.timer
```
#### Option 2: Cron
Open.
``` sh
crontab -e
```
Add to document.
``` sh
@reboot * * * * * bash-folders-battery-hook --folder [folder path]
```
---
## Development
### Check syntax

View File

@ -0,0 +1,117 @@
#!/usr/bin/env bash
# --
# Description: Script that launches other scripts in different battery states.
# --
# Cron: * * * * * bash-folders-battery-hook.sh --folder [folder path]
# --
#cat /sys/class/power_supply/BAT0/capacity
#cat /sys/class/power_supply/BAT0/status
#Discharging
#Charging
#Full
#xrandr --output eDP --brightness 1
# START
set -e
# VARIABLES
PROGNAME=$(basename "$0")
FOLDER_ORIGIN="$2"
LOW_BATTERY=15
HIGH_BATTERY=85
# FUNCTIONS
usage() {
if [ "$*" != "" ] ; then
echo "Error: $*"
fi
cat << EOF
Usage: $PROGNAME [OPTION]
Script that launches other scripts in different battery states.
"discharging" When the battery is in use.
"charging" When the battery is charging.
"low" When it reaches the low percentage. Default 15.
"high" When it reaches the high percentage. Default 85.
"full" When the battery is full.
Options:
--folder [path] Folder where the different scripts are located.
--help Display this usage message and exit
EOF
exit 1
}
send-notification() {
if command -v notify-send >/dev/null 2>&1; then
# Send a native notification
notify-send "$1"
else
# If the above command is not available, print by console
echo "$1"
fi
}
start() {
# Monitors the selected folder
inotifywait -m -e create,moved_to --format '%f' "$FOLDER_ORIGIN" |
while read -r filename; do
# Gets the file extension
extension="${filename##*.}"
# Checks if the extension is in the extension list
for ext in "${EXTENSIONS_TO_WATCH[@]}"; do
if [[ "$ext" = "$extension" ]]; then
# Check if the file name starts with "optimized"
if [[ "$filename" != optimized* ]]; then
filename_output="optimized_${filename%.*}.mp4"
# Notifies that the conversion is to be started
send-notification "Optimizing $filename_output ..."
# Displays a flat file of information
touch "$FOLDER_ORIGIN/$MESSAGE_WAITING"
# Convert the file to MP4 format using ffmpeg in /tmp/
ffmpeg -i "$FOLDER_ORIGIN/$filename" -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -nostdin -shortest "/tmp/$filename_output"
# When finished move the optimized file
mv "/tmp/$filename_output" "$FOLDER_ORIGIN/$filename_output"
# Notifies that it has been terminated
send-notification "Completed! Output: $filename_output"
# Remove a flat file of information
rm "$FOLDER_ORIGIN/$MESSAGE_WAITING"
fi
fi
done
done
}
# CONTROLE ARGUMENTS
isArg=""
while [ $# -gt 0 ] ; do
case "$1" in
--help)
usage
;;
--folder)
isArg="1"
if [ $# -eq 2 ]; then
start
else
usage "You need to specify the path of the folder to watch."
fi
;;
*)
esac
shift
done
if [ -z $isArg ] ; then
usage "Not enough arguments"
fi