Mr Transmission, please turn off the PC after all downloads are completed!

8,556

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.

Share:
8,556

Related videos on Youtube

mini
Author by

mini

Updated on September 18, 2022

Comments

  • mini
    mini over 1 year
    #!/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?

    • Oli
      Oli almost 11 years
      Oh no, that's horrible @Mitch - it runs transmission as root and would still run into this behaviour (Transmission runs the script when any download finishes - dorfa wants to shutdown after all the downloads have finished)
    • azerafati
      azerafati about 8 years
      @mini, any update on this matter?
  • somoso
    somoso almost 11 years
    Upvoted for linking to the dbus command. I've learned something new today about Ubuntu!
  • Henning Kockerbeck
    Henning Kockerbeck almost 11 years
    I haven't really looked into that script, but I'd guess it uses Transmissions web client. You can activate that in Transmissions settings. Then make sure the package php5-cli is installed to be able to use PHP on the command line. Then you should be able to call the script with something along the lines of php /where/you/stored/Transmission_Quit.php --port 9909 --hostname 127.0.0.1 --username my_user --password my_pass, of course adapted to the path you saved the PHP script to and the settings you chose for the web client.
  • mini
    mini almost 11 years
    Is it correct if I add #!/usr/bin/php -q at the beginning of that php file and renaming .php to .sh? (following this instruction)
  • Henning Kockerbeck
    Henning Kockerbeck almost 11 years
    You can, but you don't have to. That line, called a Shebang, tells your computer with which interpreter it should execute the following commands. With it, you execute the script itself and have to make sure it is executable. php Transmission_Quit.php--port... means, "call program php and let it do what is noted down in Transmission_Quit.php". In that case, you don't need a Shebang. ./Transmission_Quit.php --port... means "call Transmission_Quit.php as a program itself". Here, you do need a Shebang, to tell your computer which language the script is in. It's mostly a matter of taste.
  • Vitaly Zdanevich
    Vitaly Zdanevich over 10 years
    I tried this command from Terminal: "Error org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.Hal was not provided by any .service files"