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):
- Start the client program (KTorrent) at a particular time
- Make the program start downloading
- 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.
crontab -elets 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.ktorrentto 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=:0for 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?