Scheduling KTorrent


I just read a post titled Schedule your torrents in Ubuntu on Planet SMC. It uses the sleep command (not specific to Ubuntu) to delay the execution of the torrent client (Deluge).

I've been using another method to schedule KTorrent for the past couple of years. I'll post it here, in case anybody finds it useful.

There are three things to be done for 'autonomous' KTorrent (or perhaps any other client):
  1. Start the client program (KTorrent) at a particular time

  2. Make the program start downloading

  3. Exit the program gracefully


The first problem can be addressed by the 'sleep' method, or using cron. I prefer the latter since I wish to start the downloader everyday. Here's how to do it.

The command: crontab -e lets you edit your cron jobs. The vi-like editor allows you to specify the date/time and the command to be executed.

For example, the following entry in crontab will execute the 'cp' command at 2:10AM everyday:
10 2 * * * cp /var/log/messages /mnt/backup

You can use the same to start KTorrent, but with a small change:
10 2 * * * export DISPLAY=:0 && ktorrent

The 'export' thing is required to start any GUI application using cron.
Refer 'man 5 crontab' for more details on the crontab format.

Now comes the second part. It's not a problem if your client will immediately start all download jobs when it's started. But KTorrent doesn't do that. If you've manually paused a task, it won't start it automatically on start-up. So, how do I tell KTorrent to start downloading (in case it hasn't already)? Enter inter-process communication with The Wonderful D-Bus!

Using D-Bus, you can ask KTorrent to start all tasks. Here's the command that does the trick:
qdbus org.ktorrent.ktorrent /core startAll

If the above command doesn't work for you, try:
qdbus org.ktorrent.ktorrent /KTorrent startAll

(I've been using the latter, but since a recent system update, the D-Bus interface seems to have changed, and only the former works. You can do: qdbus org.ktorrent.ktorrent to see the functionality it exposes.)

So, to begin downloading, you first start ktorrent, wait for a few seconds (for it to come up and be live), then use the D-Bus command to make sure it's really downloading.


The third step (exiting KTorrent gracefully) is also done with D-Bus. All KDE applications (Hoorray KDE!) expose some common basic functionality via D-Bus. Exiting the application is one of them:
qdbus org.ktorrent.ktorrent /MainApplication quit

If you wish, you can stop all tasks before you ask KTorrent to quit. Figuring out how to do that is left as an exercise to the reader ;-)

To bring everything together, I use a script file which I execute at 2:10AM (yes, I too have BSNL DataOne connection) using crontab. The script starts KTorrent, waits for a few seconds (using sleep command) and then uses the DBus command to start all jobs. Another script is executed at 7:58AM that stops all tasks and after a few seconds, asks KTorrent to quit. Not bad, huh?

Important: Make sure your desktop environment (KDE/GNOME) is running before cron tries to start KTorrent. It won't work otherwise.

If you're planning to run qdbus directly from crontab (as opposed to putting everything in a script file and then running that from crontab), then you'll need export DISPLAY=:0 for the qdbus command also.

-----------------------------------


In fact, there's more to my automation setup. My BIOS has an option to start the computer at a particular time of day. So I need not keep my PC ON when I go to sleep. My PC wakes up at 1:50AM; grub loads Fedora 11; GDM comes up; it's set-up for 'timed login' after 30 seconds; KDE loads; cron fires at 2:10AM and viola!
I could even schedule a system shutdown after 8:00AM to make it fully autonomous. But wouldn't that be too much?
;-)


Comments

  1. Thanks I didn't know how to start GUI applications using cron.

    ReplyDelete
  2. Thank you.I was searching for a workable solution.Your instruction was helpful.But I have a problem.The commands work at terminal but not in crontab.I am using Ubuntu 9.10.Earlier the commands were not working.I installed kubunt-desktop and made available kde session in Ubuntu.Now the commands work in terminal.but not in crontab.What went wrong?
    -Rajeev

    ReplyDelete
  3. @Rajeev Vasu
    Which command is having problem when invoked by crontab? Is it the command to start KTorrent or the qdbus command?
    If it's the qdbus command, then try putting that in a script file and executing the script from crontab. That's how I do it, but it *should* work even if you invoke it directly from crontab.

    If it's starting KTorrent that gives you the problem, then make sure that you have logged in before you try to start KTorrent. I have auto-login enabled in my KDM (or GDM), so that KDE (or GNOME) will be running much before starting KTorrent

    ReplyDelete
  4. @Rajeev Vasu
    If you want to invoke qdbus directly from crontab, then you'll have to use export DISPLAY=:0 for the qdbus line also.

    ReplyDelete
  5. You can find de dbus ADDRSS wich ktorrent is using and then comunicate with
    >$qdbus --address ADDRSS org.ktorrent.ktorrent
    this way you can achive de comunication even from a remote conection.

    Here is how to find the ADDRSS
    >$ktpid=$(ps --no-header -o pid -C ktorrent)
    >$bus_add=$(tr '\0' '\n' < /proc/${ktpid}/environ | \
    grep DBUS_SESSION_BUS_ADDRESS | \
    sed -r -e 's/^.*ADDRESS\=//')
    >$echo $bus_add

    ReplyDelete

Post a Comment

Popular posts from this blog

Qt - Enabling qDebug messages and Qt Creator

പേര്

Dakhani Degh - Restaurant