<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Exploits of Plouj &#187; bittorrent</title>
	<atom:link href="http://www.plouj.com/blog/tag/bittorrent/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.plouj.com/blog</link>
	<description>A blog by Michael Ploujnikov</description>
	<lastBuildDate>Wed, 01 Sep 2010 17:05:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>HOWTO: run rTorrent from Cron inside Screen at boot</title>
		<link>http://www.plouj.com/blog/2008/03/31/howto-run-rtorrent-from-cron-inside-screen/</link>
		<comments>http://www.plouj.com/blog/2008/03/31/howto-run-rtorrent-from-cron-inside-screen/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 17:13:48 +0000</pubDate>
		<dc:creator>plouj</dc:creator>
				<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[bittorrent]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[hacks]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[p2p]]></category>
		<category><![CDATA[screen]]></category>

		<guid isPermaLink="false">http://www.plouj.com/blog/?p=3</guid>
		<description><![CDATA[Once I figured out how to do QoS on my openwrt router I decided that I would make use of the bandwidth provided to me by my ISP to seed some nice legal torrents as long as I can without interrupting my Internet browsing. Since I was already using rTorrent, it seemed like a good [...]]]></description>
			<content:encoded><![CDATA[<p>Once I figured out how to do <a href="http://wiki.openwrt.org/MiniHowtos/QoSHowto">QoS on my openwrt router</a> I decided that I would make use of the bandwidth provided to me by my ISP to seed some nice legal torrents as long as I can without interrupting my Internet browsing. Since I was already using rTorrent, it seemed like a good choice for a console application running inside screen that would be started by cron each time my computer boots. In theory that was true, but in practice the lack of documentation of various peculiarities cost me a few hours of digging and asking around before I had a properly working solution. In this post I will document my solution.</p>
<p>Right away I knew that I wanted to be able to easily attach to the bittorrent screen session at any time, so I used an easy to remember name for it:</p>
<pre>screen -s torrent</pre>
<p>I also knew that for screen to work with cron it had to start in a &#8220;detached&#8221; mode. Thankfully, the screen manual told me how to do that:</p>
<pre>screen -d m</pre>
<p>During my research I came across different people mentioning a <a href="http://www.google.ca/search?q=cron+screen+%22-dmS%22">short form</a> for all of the above switches:</p>
<pre>screen -dmS torrent</pre>
<p>Since I like to simplify things I immediately adopted it and unfortunately paid dearly in the end. In fact, this was one of those attractive in theory but poorly executed features that cause other things to fail and waste my time with unhelpful error messages. It took me a few hours to figure out that it was causing screen to keep failing to start from inside cron with a &#8220;Must be connected to a terminal.&#8221; error.</p>
<p>Once I got over that hurdle I tried the full command without cron:</p>
<pre>screen -d -m -S torrent /usr/bin/rtorrent</pre>
<p>Everything was good until I noticed that I couldn&#8217;t use any rTorrent keyboard commands. I couldn&#8217;t even quit it apart from killing it from a separate terminal. According to the <a href="http://libtorrent.rakshasa.no/wiki/RTorrentUserGuide">rTorrent user guide</a> this was a result of a flow control conflict with screen. Thankfully screen also had a switch -fa just for that:</p>
<pre>screen -fa -d -m -S torrent /usr/bin/rtorrent</pre>
<p>To be on the safe side I used an absolute path to screen in the cron job:</p>
<pre>/usr/bin/screen -fa -d -m -S torrent /usr/bin/rtorrent</pre>
<p>Making sure cron starts this each time my computer boots was easy with the @reboot directive so this is exactly what should go in the crontab:</p>
<pre>@reboot /usr/bin/screen -fa -d -m -S torrent /usr/bin/rtorrent</pre>
<p>After running rTorrent like this for a few days I noticed that it was a bit unstable and would crash sometimes so I came up with a simple Bash script to keep it running:</p>
<pre>
$ cat ~/bin/myrtorrent
#!/bin/bash
PROGRAM="/usr/bin/rtorrent"
GRACE_DELAY=15
while true;
do
    "$PROGRAM"
    RETURNED=$?
    if [ $RETURNED -ne 0 ]
    then
	echo "$PROGRAM did not exit cleanly with status code $RETURNED"
	echo "pausing for $GRACE_DELAY seconds before restarting $PROGRAM"
	sleep $GRACE_DELAY;
    else
	echo "$PROGRAM exited cleanly. It will not be restarted automatically"
	exit 0
    fi
done
</pre>
<p>The final cronjob looks like this now:</p>
<pre>
@reboot /usr/bin/screen -fa -d -m -S torrent $HOME/bin/myrtorrent
</pre>
<p>Sadly, the usefullness of this setup is questionable since <a href="http://arstechnica.com/news.ars/post/20080325-canadian-isps-furious-about-bell-canadas-traffic-throttling.html">Bell is throttling DSL resellers&#8217; peer-to-peer traffic</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.plouj.com/blog/2008/03/31/howto-run-rtorrent-from-cron-inside-screen/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
