The Cube - Assimilating Technology

The Cube

queManager for Transmission-daemon

For Humans only


Print E-mail
Friday, 11 September 2009 16:31

I like managing my torrents via a web interface, on a remote server.

The main reason I like this type of setup, and not a local running torrent client, is mainly to allow me to restart my computer without interupting my torrents, and also, I download them directly on my hdtv box, so no need to copy a completed file over my local network once done.
Another reason is that I can make use of remote systems. My father in law's workshop is one example, where they only used 1% of a 70 gig cap each month, but rest asured, that has been solved ;)

Ok, down to business.

I used to use Torrentflux for my web interface, but, that was a bit of an overkill, as it is mainly just me (with sometimes my brother in law, and father in law) who uses the interface. No need for all that user management et all other stuff that bittornado gave me.

Transmission Bittorent to the rescue.

It has a simple, clean interface. The only thing it lacked was a queue manager, and the ability to limit the number of concurrent torrents.

So, I created a script that runs as a cron job to manage the torrents via the transmission-remote interface.

The script does the following:

1. Restarts the daemon, if not running ( I have found mine crashes every now and then )
2. Stops all torrents that have reched 100%, is Idle, or that is just in Upload mode.  I have limited bandwidth, thus I can only share when I am myself downloading.
3. Makes sure that only X torrents run at a time (X is set in the script)
4. If no torrents are running, start X torrents ( first come first serve, so the older torrents will be started first )
5. Copy any completed torrents to a storage folder (in my case it is a seperate mounted hd), and remove them from the list

Add this as a cron job, and it will manage your torrents. I have it running every 10 minutes.

Below my code

Things to note:

The script attempts to restart the daemon, I make sure the same config script is used to that in the cron job by adding --config to the calling command. You will need to adjust that.
See the line ' /usr/local/bin/transmission-daemon --config-dir /home/web/.config' and adjust accordingly.

I am running my daemon on a non standard port (to that of the normal transmission), so you will need to adjust the REMOTE variable below. Also if you require rpc-auth, add the auth detials after.
Remember to leave one space after the command.

See the line 'REMOTE="/usr/local/bin/transmission-remote localhost:8081 "

you most probably want to remove the localhost:8081 bit.

to include auth it will look like this:

'REMOTE="/usr/local/bin/transmission-remote localhost:8081 -n username:password"

I hope this script is handy for someone else.

#!/bin/sh   

# Transmission bittorent queue managenr
# Lucas van Staden ( lvs at dedmeet.com / www.dedmeet.com )
# adapted from original found here: http://pastie.org/357236


# Path to transmission-remote
# ADD '-n username:password' to the end of the line below if you require auth
REMOTE="/usr/local/bin/transmission-remote localhost:8081 "
# Copy Completed torrents to this folder
DOCOPY=1 # set to 0 to skip the copy part
STORAGE="/home/web/Downloads/Storage"
# Maximum number of torrents that may be active at any given time
MAXACTIVE="1"

if [ -e /tmp/queueManager ]; then
logger "queueManager already active, exiting"
exit 0
fi

touch /tmp/queueManager

# is it running?
$($REMOTE -l >/dev/null)
if [ $? -ne 0 ]; then
# not running, start it
logger "Attempting to start the daemon"
/usr/local/bin/transmission-daemon --config-dir /home/web/.config
rm -rf /tmp/queueManager
exit 0
fi

## Unless youknow what you are doing, no need to edit below this line
TAILMAGIC=$( expr $MAXACTIVE + 1 )

# Stop all finished, Idle or just Uploading torrents
LIST="$($REMOTE -l | grep -E -e '100%|Idle|Uploading'  |grep -v Stopped | awk '{ print $1; }')"
for ID in $LIST; do
$REMOTE --torrent $ID --stop >/dev/null
done

# How many are still running
ACTIVE="$( $REMOTE -l | grep -E -v -e "Stopped|ETA|Sum" | wc -l )";
if [ $ACTIVE -gt $MAXACTIVE ]; then
# STop all but the first found running torrents
for ID in $( $REMOTE -l |grep -E -v -e 'Stopped|ETA|Sum' | awk '{ print $1; }' | tail -n +$TAILMAGIC ); do
$REMOTE --torrent $ID --stop >/dev/null
done
fi

if [ $ACTIVE -eq 0 ]; then
# none running, try and start one, the first one found is fine
for ID in $( $REMOTE -l |grep -E -v -e 'ETA|Sum|100%' | awk '{ print $1; }' ); do
# but did it see any seeds last time it ran?
SEEDS=$( $REMOTE --torrent $ID --info |grep seeders | awk '{ print $4; }' )
if [ $SEEDS -gt 0 ]; then
$REMOTE --torrent $ID --start --verify >/dev/null
rm -rf /tmp/queueManager
exit 0
fi     
done
#ok, se we tried to start a torrent, where seeds > 0, did that actually work?
ACTIVE="$( $REMOTE -l | grep -E -v -e "Stopped|ETA|Sum" | wc -l )";
if [ $ACTIVE -eq 0 ]; then
#nope, nothing started, so all torrents in list has last seen 0 seeds
#start them all, hopefully one of them will get some seeds,
#and the process will work again.
for ID in $( $REMOTE -l |grep -E -v -e 'ETA|Sum|100%' | awk '{ print $1; }' ); do
$REMOTE --torrent $ID --start --verify >/dev/null
done
fi
fi

if [ $DOCOPY -eq 1 ]; then
#Now move all finished torrents to the storage, and remove from the torrent list.
LIST="$($REMOTE -l | grep 100% | awk '{ print $1; }')"
for ID in $LIST; do
#Make sure it is stopped
$REMOTE --torrent $ID --stop >/dev/null
NAME=$( $REMOTE --torrent $ID --info |grep Name: )
NAME=${NAME#*Name: }           
LOCATION="$( $REMOTE --torrent $ID --info |grep Location: )"
LOCATION=${LOCATION#*Location: }               
mv "$LOCATION/$NAME" "$STORAGE/" >/dev/null
# remove this torrent from our list, but only if no error was detected on the mv       
if [ $? -eq 0 ]; then
$REMOTE --torrent $ID --remove-and-delete >/dev/null
else
logger "Tried to move $LOCATION/$NAME to $STORAGE, got exit code $? "
# not clearing up the lock file
# if there was an error, then we don't want to have it repeat,
# else we will get a lot of 'job failed' cron messages emailed
# this will require some manual intervention
exit $?
fi
done
fi

rm -rf /tmp/queueManager
exit $?
Last Updated on Monday, 23 November 2009 20:35
 
Share on facebook