0

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?

1 Answers1

0

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.

Each time run transmission-remote once, use one awk to fully process the output. If I read your script right, you don't need sort nor head, nor even similar functionalities of awk. The relevant code may be like:

query_transmission | awk 'NR!=1 && $1!="Sum:" && $2!="100%" {print "busy"; exit}'

where query_transmission is a shell function (instead of $trans; see How can we run a command stored in a variable?). The snippet will print busy iff there is a non-first, non-last line that does not report 100% in the second column; otherwise the output will be empty. The last line of output from query_transmission is detected by comparing the first field to Sum:; this is not really elegant, but detecting the last line in awk in general is not elegant either.

You also don't need find. The following line will tell you the number of files in $dir (I assume the variable is set and not empty), using only the shell, i.e. without external tools:

(shopt -s dotglob nullglob; set -- "$dir"/*; echo "$#")

(a subshell, so shopt and set do not affect the main shell).

Therefore I think the condition after your while can be simplified to:

[ -n "$(query_transmission | awk 'NR!=1 && $1!="Sum:" && $2!="100%" {print "busy"; exit}')" ] \
|| (shopt -s dotglob nullglob; set -- "$dir"/*; [ "$#" -gt 0 ])

In your script you should quote right.