Ubuntu Hardy Schedule Bittorrent Downloads
simon — Tue, 05/13/2008 - 10:48
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 Hardy 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
When the text editor appears enter the following and hit save:
#!/bin/sh # Start Downloading Torrent Files! cd btlaunchmany /home/(Your Username)/Torrents/ > torrent.log & tail -f torrent.log
I'll break this down line by line.
The first line defines the file as a bash file.
The second line is a comment. It simply states what the file does.
The third line is a Change Directory command.
The fourth line executes the Bittorrent application, downloads your torrents into a new directory within your Torrents directory, and creates a log file in your home directory.
The Fifth line renders the last few lines of the torrent.log file in the terminal. This function does not occur with the method I am outlining below so unless you will execute this file directly then leave off the fifth line and the '&' at the end of the fouth line.
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
When the text editor appears enter the following and hit save:
#!/bin/bash # Stop Downloading ALL Torrent Files! killall btlaunchmany
I'll break this down again.
The first line defines the file as a bash file.
The second line is a comment. It simply states what the file does.
The third line terminates any instances of your bittorrent application launched via the first file.
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), and
sh /home/(Your Username)/bash_scripts/bittorrentstop.sh
which will stop the downloads and, ideally, exit the terminal too (only in some setups).
(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 folled 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.











Hey Simon, I'll put a link to
Raymond (not verified) — Wed, 05/21/2008 - 23:04Hey Simon, I'll put a link to your blog on mine. Is this your own domain via OpenID??
Raymond
Re: Raymond
simon — Sun, 05/25/2008 - 14:29I'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.
Post new comment