wiki:Scripts/Watchdog

Version 1 (modified by Waldorf, 12 years ago) (diff)

--

Watch Directory Script

Notes

To run this script at an interval, add this to your crontab.

Script "watchdog.sh"

#!/bin/bash
# 

# Authentication "username:password"
#tr_auth="admin:admin"

# Watch dir, may contain spaces:
watchdir="/torrentwatch/"

# move file to a subdirectory? if Commented out, it'll be removed.
# *Don't put a '/' before the path*
movesubdir="added/"

# Verbose?
verbose=1

for file in "$watchdir"*.torrent
do
    if [ -f "$file" ]; then
        if [ $verbose ]; then echo "$file added to queue."; fi
    else
        if [ $verbose ]; then echo "No torrent in $watchdir."; fi

        if [ $tr_auth ]; then
            /usr/bin/transmission-remote --auth="$tr_auth" -a "$file"
        else
            /usr/bin/transmission-remote -a "$file"
        fi
        
        # give the remote some time to process
        sleep 5
        
        if [ $movesubdir ]; then
            if [ -d "$watchdir$movesubdir" ]; then
                mv "$file" "$watchDir$movesubdir"
            else
                mkdir "$watchdir$movesubdir"
                mv "$file" "$watchdir$movesubdir"
            fi
        else
            rm "$file"
        fi
    fi
done

exit 0