Multiple watch folders script

Discussion of Transmission that doesn't fit in the other categories
Post Reply
dlbogdan
Posts: 3
Joined: Fri Feb 28, 2014 6:31 pm

Multiple watch folders script

Post by dlbogdan »

Hi,
Already posted this but on the wrong subforum (Mac requests).
I've managed to write a simple script in 5 minutes (a bit dirty but it works) for adding the multiple folder watch functionality to transmission-daemon (on linux).
It's using inotify-tools.

Code: Select all

#!/bin/bash
inotifywait -q -r -m $1 --format %w%f -e create |
while read file; do
   if echo "$file" | grep -iq "\.torrent" ; then
     canonfile=$(readlink -f "$file")
     dir=$(dirname "$canonfile")
     if echo `basename "$file"` | grep -iq "^\._"; then
      echo
      else
      #echo "Waiting for the file to close"
      while : ; do 
        a=`stat -c%s "$canonfile"`
      sleep 0.5s
      if [ $a -gt 0 ]; then break; fi
      done
      while : ; do
        a=`stat -c%s "$canonfile"`
      sleep 5s
      if [ $a -eq `stat -c%s "$canonfile"` ]; then break; fi
      done
      transmission-remote -a "$canonfile" -w "$dir" 
      rm -f "$canonfile"
     fi
   fi
done
As always with watching files from an external process you never know when will it finish writing, given you have no signal from the writing process. So race conditions appears. I've mitigated that by watching file grow until it stops. I'm watching the file first get passed zero because in my experiments sometimes it will stay to zero for a couple of seconds before starting to grow.
No, using close_write event will not do it, for example samba will close and reopen the file multiple times during a transfer..
Note: This part " grep -iq "^\._";" is for filtering out the crap Mac OS X will write on a samba share along with the file you copy.

So basically you'll lunch this script from ex. /etc/rc.local with the parameter of the root folder you want to watch. It will recursively watch all the subfolders from then on for .torrents file. This script can be modified to watch only a specific folder (and ran multiple times with different folders to selectively watch a number of folders) by just modifying the first line "inotifywait -q -r -m $1 --format %w%f -e create |" by removing the "-r" parameter.

Cheers!
dlbogdan
Posts: 3
Joined: Fri Feb 28, 2014 6:31 pm

Re: Multiple watch folders script

Post by dlbogdan »

Improved version

Code: Select all

#!/bin/bash
inotifywait -q -r -m $1 --format %w%f -e create |
while read file; do
        if echo "$file" | grep -iq "\.torrent" ; then
                canonfile=$(readlink -f "$file")
                dir=$(dirname "$canonfile")
                if echo `basename "$file"` | grep -iq "^\._"; then
                        echo
                else
                        counter=0
                        while : ; do
                                if [ "$counter" -gt "10" ]; then break; fi
                                sleep 0.5s
                                a=`stat -c%s "$canonfile"`
                                if [ "$?" -ne "0" ]; then break; fi
                                echo "$canonfile" stat: "$a"
                                if [ "$a" -ne "0" ]; then break; fi
                                counter=`expr "$counter" + 1`
                        done
                        while : ; do
                                a=`stat -c%s "$canonfile"`
                                if [ "$?" -ne "0" ]; then break; fi
                                sleep 5s
                                if [ "$a" -eq `stat -c%s "$canonfile"` ]; then break; fi
                        done
                        a=`stat -c%s "$canonfile"`
                        if [ "$?" -eq "0" ]; then
                                transmission-remote -a "$canonfile" -w "$dir";
                        fi
                rm -f "$canonfile"
                fi
        fi
done
k3smission
Posts: 1
Joined: Wed Mar 19, 2014 9:33 pm

Re: Multiple watch folders script

Post by k3smission »

This lack of feature is the only reason why i still keep rtorrent.
I use this feature to order torrents according to the tracker where it's registred.
When i download a torrent with my web browser, a ruby script run to extract the announce url and then the tracker url where the torrent is registred and then send it to my server in the correct watch folder (the script could run on my server though, but for some reasons i don't want it, anyway that's off-topic here).
And when the download of the torrent is finished the file is moved.

I have these folders :

data/watch -> and as many sufolders as i use number of trackers
data/incomplete -> and as many sufolders as i use number of trackers
data/complete -> and as many sufolders as i use number of trackers

This is clearly not optional for me, it's a must have.

My 2 cents, cheers.
Post Reply