Transmission Shutdown Script for Multiple Torrents?

I have written a shutdown script for transmission. Transmission calls the script after a torrent download finishes. The script runs perfectly on my machine (Ubuntu 11.04 & 12.04).

#!/bin/bash
sleep 300s

# default display on current host
DISPLAY=:0.0

# find out if monitor is on. Default timeout can be configured from screensaver/Power configuration.

STATUS=`xset -display $DISPLAY -q | grep 'Monitor'`
echo $STATUS

if [ "$STATUS" == "  Monitor is On" ]

###  Then check if  its still downloading a torrent. Couldn't figure out how.(May be) by monitoring network downstream activity? 

then

    notify-send "Downloads Complete" "Exiting  transmisssion now"
    pkill transmission

else
    notify-send "Downloads Complete" "Shutting Down Computer"
    dbus-send --session --type=method_call --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.RequestShutdown

fi

exit 0

The problem is that when I'm downloading more than one file, when the first one finishes, transmission executes the script. I would like to do that but after all downloads are completed.

I want to put a 2nd check ( right after monitor check) if it is still downloading another torrent.

Is there any way to do this?

3 Answers

This information isn't passed to the scripts via environment variables, so you'll need to query Transmission's RPC interface. This is sometimes done by client libraries -- for example, a Python script could use python transmissionrpc. There are other interfaces like this listed at .

Here's a quick approach that will use transmission-remote to count the number of non-idle downloads:

transmission-remote yourhost:yourport -tall --info | grep "^  State:" | grep "Down" | wc --lines

If you want to include idle downloads too, you could try this:

transmission-remote yourhost:yourport -l | grep -v -e " 100% " -e "^Sum" -e "^ID" -e " Stopped " | wc --lines

Where "^ID" and "^Sum" strips the header and footer; " 100% " strips completed torrents; and " Stopped " strips out the incomplete-but-paused torrents. This approach isn't foolproof -- for example, a torrent named " 100% Stopped " would break it.

1

I created a better script(from user98677 advice) which takes advantage of transmission's RPC Interface.

Code:

Github Gist:

What it Does?

  1. Pause or remove completed torrents after they are completed.

  2. Send a pushover notification (with curl)[optional]

  3. Send a twitter notification (requires twidge)[optional]

  4. Suspend/Shutdown Computer OR leave it as it is.

Screenshot


Setup

On Ubuntu

sudo apt-get install libnotify-bin
sudo apt-get install transmission-cli

On Ubuntu >= 13.04 (For twitter notification):

sudo add-apt-repository ppa:moorhen-core/moorhen-apps
sudo apt-get install twidge

For suspend action on non-Ubuntu distro (Ubuntu uses Upower) install powermanagement-interface package

sudo apt-get install powermanagement-interface

After installation:

  1. Get the code from github-gist & save the file as trsm anywhere on your hard-drive. Make the file executable chmod a+x trsm .

  2. Login to pushover & copy your user key. Paste it under user-key in the script.

  3. If you want notifications send with nice looking application (transmission) icon just create a fake app in pushover with transmission icon & then copy the application key (API/Token key) & paste it under app-key in the script.

  4. For twitter setup see twidge documentation.

  5. Open transmission. Go to preference->web. Enable web client (default port 9091) & enable user authentication . Choose a username & password. Put that username & password in the script as username & password respectively.

  6. Click open web-client to check if it is working properly.

  7. Finally save the script & go to downloading tab (in transmission preference) & click call script when torrent is complete. Select the respective script.


Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.