Ryan and Debi & Toren

Linux: Starting and Stopping Torrents from Crontab

I download my Kubuntu installation images via Bit Torrent and like to keep Bit Torrent running most of the time. However, there are occasionally times when I want to pause my torrents and then restart them (e.g., when I’m streaming videos or otherwise using lots of bandwidth). I can, of course, do this manually, but I was wondering how I might start and stop my torrents automatically.

Luckily, the torrent manager I use, qBittorrent, has the ability to receive commands via a web UI which means I can start and stop torrents using Cron. Here’s how I made that work.

In order to stop and start your torrents, you’ll need first to enable the web interface in qBittorrent:

Set up a username and password. You should also set qBittorrent to “Bypass authentication for clients on localhost” and/or “Bypass authentication for clients in whitelisted IP subnets” and then enter the IP address you want to bypass authentication.

I tried to turn off authentication for clients on my localhost but didn’t realize that it worked because I was logging in using my IP address and the port (e.g., 192.168.1.1:8080) instead of just “localhost:8080”. So, I figured out an extra step that turned out not to be necessary: how to grab a cookie that I could then use to authenticate when I sent commands to the web interface. Here’s how I did that. Open a terminal on your device and enter the following command once you’ve turned on the web interface and set a username and password:

curl -i header 'Referer: http://localhost:8080' --data 'username=admin&password=[passwordhere]' http://localhost:8080/api/v2/auth/login

Obviously, enter your password where I wrote [passwordhere]. In the resulting output, you’ll see the cookie:

set-cookie: SID=[randomcharactershere]

Copy the “SID=[randomcharactershere]” portion. You’ll need that for the commands we’ll use in the cron job if you’re logging in remotely or didn’t set qBittorrent to bypass authentication on the localhost.

Now, to automate this. Open up your user’s crontab. From a console, enter:

crontab -e

Inside, we’re going to create two entries. First, to start all your torrents at 1:00 am every day (see here for setting times for crontab) assuming you need the cookie, you would add this command:

0 1 * * * curl http://localhost:8080/api/v2/torrents/resume --data 'hashes=all' "SID=[randomcharactershere]"

If you set qBittorrent to bypass authentication for clients on localhost, you can change the above command to just leave out the last part, like this:

0 1 * * * curl http://localhost:8080/api/v2/torrents/resume --data 'hashes=all'

Let’s say we want to stop the torrents at 4:00 am. When we want to stop the torrents, we use a very similar command, but with one change:

0 4 * * * curl http://localhost:8080/api/v2/torrents/pause --data 'hashes=all' "SID=[randomcharactershere]"

And, again, if you have bypassed authentication on your localhost, you can use a slightly shorter version without the cookie at the end:

0 4 * * * curl http://localhost:8080/api/v2/torrents/pause --data 'hashes=all'

The above commands will start your torrents at 1:00 am and stop them at 4:00 am every day. You can obviously change the times. There are additional commands you can use with your torrents, like starting and stopping specific torrents. See here for a complete list. I also found this forum discussion helpful.

Exit mobile version