wiki:Scripts/Watchdog

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

This is going to run often, so silence the remote output

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 removed remove
# the torrent file.
# Note: 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

        if [ $tr_auth ]; then
            /usr/bin/transmission-remote --auth="$tr_auth" -a "$file" > /dev/null
        else
            /usr/bin/transmission-remote -a "$file" > /dev/null
        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
    else
        if [ $verbose ]; then echo "No torrent in $watchdir."; fi
    fi
done

exit 0