Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
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?

share|improve this question
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) – Oli Apr 30 '13 at 8:40
@Oli Sorry, after reading it all the way, yeah.. I went ahead and deleted the comment. – Mitch Apr 30 '13 at 8:46

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 \

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|improve this answer
Upvoted for linking to the dbus command. I've learned something new today about Ubuntu! – somoso Apr 30 '13 at 9:16
I tried this command from Terminal: "Error org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.Hal was not provided by any .service files" – Vitaly Zdanevich Jul 8 '13 at 9:29

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.

share|improve this answer
-->> a php script! how it works? – mini Apr 30 '13 at 18:08
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 --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. – Henning Kockerbeck Apr 30 '13 at 23:47
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) – mini May 1 '13 at 12:03
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. – Henning Kockerbeck May 1 '13 at 14:06

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.