3
#!/bin/bash
echo "password" | sudo -S halt

I pointed this script to "Call script when torrent is completed" option under: Edit > Preferences > Downloading

Problem: this script shut-downs the computer after any finished download while there are more torrents in queue!

How can I improve it?

mini
  • 2,385

3 Answers3

4

Firstly - stop shutting down with root. Use dbus.

Next, put something in your script that detects running torrents. Here is a little something that uses trasmission-remote to count the number of torrents running that aren't "Done":

transmission-remote --list | sed '1d;$d' | grep -v Done | wc -l

To build that into your script:

count=$(transmission-remote --list | sed '1d;$d' | grep -v Done | wc -l)
if [ $count -eq 0 ]; then
    dbus-send --system --print-reply --dest=org.freedesktop.Hal \
        /org/freedesktop/Hal/devices/computer \
        org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown
fi

I'm not a transmission user so my search might be slightly off but this should do the job. You might find that it doesn't shut down all the time if there are some torrents in there that are, for example, paused. If that's the case, play around with the output and a grep -v clause or two to handle things.

Additionally, you might want a timed shutdown so you're never in a situation where the desktop shuts down while you're on it (so you can abort it). Perhaps just an additional check in there to see what the time is.

Note: transmission-remote requires you turn on web access to transmission from within its options.

Oli
  • 299,380
0

Transmission offers an RPC interface that let's you communicate with Transmission programmatically. With that, you could write a script that is executed after one (any) torrent is finished downloading, as you do now. That scripts checks through the RPC interface if there are any other torrents still downloading. Only if none are found, the system is shut down, else the script exits without doing anything (and waits to be called again once the next torrent finishes).

I'm not sure whether you can talk to the RPC interface in a bash script, but according to the Transmission Homepage there are "remote control libraries" to use in Ruby, Python, PHP or Perl. So it shouldn't be to hard to write a script in any of those languages to do the task described above.

0

Like @Henning said, make use of the transmissionrpc to get the status of all the torrents. If none of them are still active then you can go ahead and shutdown your computer.

I've written a little program in Python to do this. You can find it here.

rohithpr
  • 157