I'll try and keep this as brief as I can, whilst supplying all the required information.
I would like the red LED on the front of my NAS running Transmission on Debian to flash red whilst downloading a torrent, and then turn white when all torrents are finished.
I'm trying to plan a script that's more efficient than the original one:
#!/usr/bin/env bash
user=my_username
pass=my_password
dir=/Hitachi/Downloads/Temp/
trans="transmission-remote 0.0.0.0:9092 -n $user:$pass -l"
pid_file="/var/run/process.pid"
if [ -f $pid_file ]; then
exit
else
echo $$ > $pid_file
fi
flash_red () {
echo 0 > /sys/class/leds/dart:white:power/brightness
while true
do
echo 1 > /sys/class/leds/dart:red:power/brightness
sleep 0.5
echo 0 > /sys/class/leds/dart:red:power/brightness
sleep 0.5
done
}
flash_red &
while
[ "$($trans | awk '! /awk/ && /n[/]a/ {print $2}')" == "n/a" ]
|| [[ $($trans | awk '! /awk/ && /%/ {print substr($2, 1, length($2)-1)}' | sort -n | head -n 1) -lt 100 ]]
|| find $dir -mindepth 1 -maxdepth 1 | read
do
sleep 10
done
kill $!
echo 0 > /sys/class/leds/dart:red:power/brightness
echo 1 > /sys/class/leds/dart:white:power/brightness
rm $pid_file
exit
It's rather inefficient because every 10 seconds it has to probe 'transmission-remote' twice, run awk twice, and run sort, head and find once.
Transmission-remote outputs in a table format that needs to be sorted to get the desired result.
Transmission does have a feature where it will run a script on torrent add, and run a different script on torrent completion.
Could anyone think of a way I could utilise this feature (or any other way, aside from changing the 10 seconds to another timing) to make the flash feature less system intensive?