Ubuntu Hardy Schedule Bittorrent Downloads

This week my Internet connection was shaped to 64k during on-peak times.  This means that from 12:00 PM until 2:00 AM I can't really download anything without it causing a major headache.  This led me to wonder if I could set up the bittorrent client that comes with Ubuntu to automatically execute only during the off-peak times (from 2:00 AM until 12:00 PM) in a fairly intuitive manner.  I don't want my torrent downloads slowing down my on-peak net usage!  I've figured it out and the following describes how I achieved it.

To begin create a new Directory in your home directory called 'torrents'.  To do this from the terminal type:

mkdir -p /home/(Your Username)/torrents

Next create another new directory in your home directory called 'bash_scripts' (I always use an underscore for directory names and file names that have spaces, it makes working with them in the terminal that much easier).  To do this from the terminal type:

mkdir -p /home/(Your Username)/bash_scripts

We need to write a bash script to start the bittorrent application and another bash script to stop the bittorrent application.

To create the first bash script, the one that will start the bittorrent application, type the following in the terminal:

gedit /home/(Your Username)/bash_scripts/bittorrentstart.sh

Or if you want to remain in the terminal just use your favorite text editor such as vim or nano.

When the text editor appears enter the following and save it (or click here to download the file):

#!/bin/sh
# Start Downloading Torrent Files!
# Written by Simon Ives - http://www.simonives.info
#
echo "Starting the Download Torrents Script"
#
echo "Checking for Torrent files"
#
if [ -f /home/Your_User_Name/torrents/*.torrent ]; then
echo "Executing Bittorrent Application and saving progress to torrent.log"
nohup btlaunchmany /home/Your_User_Name/torrents > /home/Your_User_Name/torrents/torrent.log &
else echo "No Torrent files found"
fi

This script will search your torrents directory for one or more torrent files and if there is one or more present it will execute the bittorrent application.

To create the second bash script, the one that will stop your bittorrent application, type the following in the terminal:

gedit /home/(Your Username)/bash_scripts/bittorrentstop.sh

Or if you want to remain in the terminal just use your favorite text editor such as vim or nano.

When the text editor appears enter the following and hit save (or click here to download the file):

#!/bin/bash
# Stop Downloading ALL Torrent Files!
# Written by Simon Ives - http://www.simonives.info
echo "Stopping all bittorrent downloads"
killall btlaunchmany

This script simply stops all bittorrent downloads

Next we need to define these files as executable.  To do this enter the following in the terminal (one at a time):

chmod +x /home/(Your Username)/bash_scripts/bittorrentstart.sh
chmod +x /home/(Your Username)/bash_scripts/bittorrentstop.sh

Great, now we have two bash scripts that will start downloading our torrents and stop downloading our torrents when called.  There is one important aspect that needs attention and makes the process of downloading files via torrents even easier.  The bittorrentstart bash script that has been created will scan the 'torrents' directory for *.torrent files and start downloading all files present.  Further, it will automatically create a new subdirectory for each download.  So you simply save all of your *.torrent files in the /home/(Your Username)/torrents directory and every time the bittorrentstart bash script is executed it will automatically begin their download.

OK.  Now that we have our scripts we need to setup a system where they will be executed at the correct time.  We do this with the cron application.  Cron executes files at certain times defined by the user.  My off-peak download time is between 2:00 AM and 12:00 PM so I need to let cron know to execute the first bash script after 2:00 AM and to execute the second bash script before 12:00 PM.  To do this I create a text file called cron.txt in my home directory so that I don't have to manually edit cron's entries directly.  To do this enter the following in the terminal:

gedit /home/(Your Username)/cron.txt

You'll be presented with a blank text document that we need to edit with the times that we want our scripts to be executed and their location.  My cron.txt file appears as follows:

# Start BitTorrent Download Script
05 02 * * * sh /home/(Your Username)/bash_scripts/bittorrentstart.sh
# Stop ALL BitTorrent Downloads Script
55 11 * * * sh /home/(Your Username)/bash_scripts/bittorrentstop.sh

I'll break down the first entry which will also explain the second.

The first line is a comment.  It lets you know what is being done.
The second line is the cron entry itself.  05 is the minutes, 02 is the hour, and the other three asterisks are wilcards that are in place of the day, month, and year columns.  The remainder, beginning from sh, tells cron to execute the bash script.  This entry will execute the bash script bittorrentstart.sh at 2:05 AM on every day, of every month, of every year.  Cron entries are in 24 hour time so if you want a particular line to begin at 8:15 PM, for example, then the time colums would read - 15 20 * * *.

We need to load this file into cron and to do this we enter the following in the terminal:

crontab /home/(Your Username)/cron.txt

And that's it.  Now, at 2:05 AM every day any *.torrent files in the /home/(Your Username)/Torrents directory will automatically download.  Should this process not be complete by 11:55 AM all torrent downloads will be terminated.

TROUBLESHOOTING

Feel free to contact me if you are having problems with the above method (comments below best).

If this isn't working for you then go through the following steps to isolate the problem.

1. After you've created the first and second bash scripts execute them, one at a time.  You can simply double click on each file and hit 'run in terminal' if you like or you can execute them from the terminal.  To do the latter enter:

sh /home/(Your Username)/bash_scripts/bittorrentstart.sh

for the first bash script (you should see a series of log file read-outs if it's working), and

sh /home/(Your Username)/bash_scripts/bittorrentstop.sh

which will stop the downloads.

(To get a new terminal tab -you can't type in the original terminal window while the first script is active- hold ctrl-shift-t)

If these scripts are working then you have a cron error.

2. Follow the above steps to create the cron.txt file.  Once created and you've followed the steps to load the cron.txt file into cron enter the following in the terminal:

crontab -l

(that's an l for Larry)  You should see your cron entries on the screen.  If you don't, or you're still having problems, then please post a comment below.

Trackback URL for this post:

http://archive.simonives.info/trackback/101

Comments

Hey Simon, I'll put a link to

Hey Simon, I'll put a link to your blog on mine. Is this your own domain via OpenID??

Raymond

Re: Raymond

I'll add you to my blog roll today Raymond.

This is my own domain but it's not through OpenID.  You can just sign-up at OpenID and add any domains you opperate to your account.

n/a

reply

Writing scripts is quite fun if you have time to kill, but if you want to tell other people how to easily do the same thing why not save a lot of words and just say:sudo apt-get install deluge deluge Edit > Plugins > Scheduler.

Re: Deluge

@alarm systems
For many reasons, most notably if the user is using a headless server.
Also, I assume you mean "sudo apt-get install deluge-torrent"

It's better to debate a question without settling it than to settle a question without debating it. - Jeseph Joubert

Pause/Continue unfinished torrents

Hey, thanks so much for sharing, have exact same dilemma and what to make most of 'offpeak' bonus downloads...
A quik question, will this 'pause' incomplete torrents and restart them upon next 'start' time? 
 
IE My concern is that some larger torrents can take 2-3 days to download, whilst this process will take even longer (possibly) it would be great to know they will start/stop within offpeak, then recommence in teh next offpeak period etc.
 
Does this make sense..I'm not into scripting but the 'KILLALL' statemetn has me thinking it STOPS them vs PAUSE.
Many thanks
GibberHead

Re: Pause Torrents

This process doesn't pause the torrent download.  But, unless you muddle with partly downloaded torrent file, it will start downloading from where it left off.  You can log out or shut down the computer, it doesn't matter, the next time the script is called it will start from where it left off.

It's better to debate a question without settling it than to settle a question without debating it. - Jeseph Joubert