Enhanced CTorrent and CTCS

From NAS-Central Buffalo - The Linkstation Wiki
Revision as of 22:07, 28 January 2007 by (Talk) (Automatic downloading of torrents)

Jump to: navigation, search

Enhanced CTorrent is a BitTorrent client implemented in C++ to be lightweight and quick and is the continuation of its parent CTorrent.

Enhanced CTorrent has an average CPU load of 4.2% at a download rate of 100KB/s and an upload rate of 100KB/s on an ARM equiped Linkstation Pro.

LSPro (arm9) - OpenLink

Building from scratch

$ gunzip ctorrent-1.3.4-dnh2.2.tar.gz
$ tar xvf ctorrent-1.3.4-dnh2.2.tar.gz
  • If you are compiling in Scratchbox make sure to have libstdc++.so installed in for example /usr/lib. Either copy it from your Linkstation directly to the Scratchbox environment by using scp or install it from anywhere else.
  • Enhanced CTorrent has very little dependencies therefore you should be able to do a simple:
$ ./configure
$ make

Pre-built binary

A pre built ARM binary ctorrent is available from LSPro Temporary Upload Folder

CTorrent Control Server (CTCS)

To control and manage your torrents from an external computer you can make use of the Web interface provided by the CTorrent Control Server (CTCS). This is a Perl script which needs no further configuration.

Typical Setup

  • Make sure the binary ctorrent is available via your PATH environment varaiable.
  • Start the CTorrent Control Server and publish the server at port 2780:
$ ctcs -p 2780 > /dev/null &
  • Start ctorrent as a backgorund process downloading torrent.torrent subscribing at the locally running CTCS server at port 2780 and ignoring any console output since we will be controlling it via a web-interface.
$ ctorrent -S localhost:2780 torrent.torrent > /dev/null &
  • On the remote system open up a webbrowser and surf to:

For further information on how to start, stop and manage ctorrent please read the CTorrent user's guide.

Automatic downloading of torrents

It is very desireable to start a torrent more userfriendly at a remote machine than to manually:

  1. Download the torrent on the remote machine to a Linkstation share.
  2. Do a remote login to the Linkstation.
  3. Start ctorrent with the correct command line parameters.

To semi-automate these tasks, the Bash script below will check a specified directory on your Linkstation for any newly added .torrent files and will spawn a new ctorrent process for it when the torrent was not already activated previously. This way you can start downloading a torrent simply by copying a .torrent file to a certain shared directory on your Linkstation.


#                 Activate/deactivates torrents Bash shell script             #
# Author: Sander van Woensel
# Description: Starts and stops ctorrent for any newly added or removed
#              .torrent file found or not found in the path specified on the
#              command line.

#                                  Constants                                  #


LOG_FILE="/var/log/$(basename $0 .sh).log"

ERROR_NO_CTORRENT="Cannot execute ctorrent. Make sure ctorrent is available via one of the\n       paths in your PATH environment variable."
ERROR_INVALID_TORRENT_DIR="Invalid .torrent file directory specified."

#                                  Functions                                  #

# Prints script usage.
function usage
printf "Usage: $0 <path>

Spawns a ctorrent process for any new .torrent file in <path> not already
actiavted previously.

     path: Directory to check for newly added .torrent files.


function log
   echo "[$(date -R)] $@" >> "$LOG_FILE"

function error
   printf "Error: $*\n\n"
   exit $EXIT_ERROR

# Waits until file ($1) does not change in size (and thus is fully copied).
function wait_till_file_uploaded
   while CURRENT_SIZE=$(ls -ld "$1" | awk '{print $5}') && [ $CURRENT_SIZE != $SIZE ]; do
      sleep 2

# Spawn ctorrent for every new torrent file found.
function activate_new
   PROCESSING=$(ps -df | awk '$8 ~ /ctorrent/ {for(i=11; i<=NF; i++) { printf $i" ";}}')
   # Start new torrents found in $1.
   # Take into account file names with spaces (read command reads one full line at the time).
   ls -d1 *.torrent *.tor 2>/dev/null | while read FILE; do
      if [ -f "$FILE" ] && [ -r "$FILE" ]; then
         # If file is NOT found in the currently processing file list, then
         # spawn a ctorrent process.
         # Uses grep -F to interpret $FILE as a fixed string   
         ACTIVATED=$(echo "$PROCESSING" | grep -F "$FILE")
         if [ "x$ACTIVATED" == "x" ]; then
            wait_till_file_uploaded "$FILE"
            log "Spawning ctorrent: ctorrent -S localhost:$CTORRENT_PORT \"$FILE\" &"
            ctorrent -S localhost:$CTORRENT_PORT "$FILE" > /dev/null 2>> "$LOG_FILE" &

# Kills every ctorrent process from which the .torrent file has been removed.
function deactivate_removed

   ps -df | awk '$8 ~ /ctorrent/ { printf $2" "; for(i=11; i<=NF; i++) { printf $i" ";}; printf "\n";}' | while read PID FILE; do
      # If the .torrent file belonging to the currently activated ctorrent process has been
      # removed, stop the ctorrent process.
      if [ ! -f "$FILE" ]; then
         log "Terminating ctorrent for \"$FILE\": kill -TERM $PID"
         # Send the terminate signal to the correct ctorrent process (clean shutdown).
         kill -TERM $PID 2>> "$LOG_FILE"

#                                    Main                                     #

# Did the user requested the help text?
if [ $# != 1 ] || [ "$1" == -h ] || [ "$1" == --help ]; then
   exit $EXIT_OK

# Check if ctorrent can be executed.
if [ "x$(which ctorrent)" == "x" ]; then

# Check if the specified directory is available and can be read.
if [ $# != 1 ] || [ ! -d "$1" ] || [ ! -r "$1" ]; then

# Change directory to the .torrent directory. This removes the need
# to concatenate any paths when executing ls ,etc.
cd "$1"

# All tests succeeded; spawn ctorrent.
activate_new "$1"
deactivate_removed "$1"


  • Login your Linkstation as "root" via ssh or telnet.
  • Copy the above text in a new file on your linkstation and save it as /etc/cron/cron.d/activate_torrents.sh
  • Make sure the script is executable for the user "root":
# chmod u+x /etc/cron/cron.d/activate_torrents.sh
  • Add the script to the cron table so it gets executed at a specified interval. The following command will open up the cron table in your default editor:
# crontab -e
  • Add the line mentioned below to the end of the crontab file:
*/05 * * * * /etc/cron/cron.d/activate_torrents.sh /mnt/disk1/<share>/torrents
  • This will check for new .torrent files in the directory /mnt/disk1/<share>/torrents every 5 minutes and will start a new ctorrent session when necessary.
  • On your computer copy a .torrent file to the share: <share>/torrents, wait 5 minutes and check if the ctorrent process started downloading by executing:
# ps -df | grep ctorrent
  • You can also check CTCS by browsing to: http://<linkstation-ip>:2780.

Related articles