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 0The 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 --linesIf 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 --linesWhere "^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.
1I created a better script(from user98677 advice) which takes advantage of transmission's RPC Interface.
Code:
Github Gist:
What it Does?
Pause or remove completed torrents after they are completed.
Send a pushover notification (with curl)[optional]
Send a twitter notification (requires twidge)[optional]
Suspend/Shutdown Computer OR leave it as it is.
Screenshot
Setup
On Ubuntu
sudo apt-get install libnotify-bin
sudo apt-get install transmission-cliOn Ubuntu >= 13.04 (For twitter notification):
sudo add-apt-repository ppa:moorhen-core/moorhen-apps
sudo apt-get install twidgeFor suspend action on non-Ubuntu distro (Ubuntu uses Upower) install powermanagement-interface package
sudo apt-get install powermanagement-interfaceAfter installation:
Get the code from github-gist & save the file as
trsmanywhere on your hard-drive. Make the file executablechmod a+x trsm.Login to pushover & copy your user key. Paste it under
user-keyin the script.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-keyin the script.For twitter setup see twidge documentation.
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 asusername&passwordrespectively.Click open web-client to check if it is working properly.
Finally save the script & go to downloading tab (in transmission preference) & click
call script when torrent is complete. Select the respective script.
Script
#!/bin/bash
user-key=" " #put your pushover user-key
app-key=" " #put your pushover application-key
device=" " #Your device name in pushover
username=" " # Transmission remote username
password=" " # Transmission remote password
sleep 100s
# default display on current host
DISPLAY=:0.0
# authorize transmission
trsm="transmission-remote --auth $username:$password"
# find out number of torrent
TORRENTLIST=`$trsm --list | sed -e '1d;$d;s/^ *//' | cut --only-delimited --delimiter=' ' --fields=1`
for TORRENTID in $TORRENTLIST
do echo "* * * * * Operations on torrent ID $TORRENTID starting. * * * * *" #echo $TORRENTID DL_COMPLETED=`$trsm --torrent $TORRENTID --info | grep "Percent Done: 100%"` #echo $DL_COMPLETED
# pause completed torrents & get those torrent names. if [ "$DL_COMPLETED" != "" ]; then $trsm --torrent $TORRENTID --stop trname=`$trsm --torrent $TORRENTID --info | grep "Name:" | awk -F: '{print $NF}'` # post an update to twitter echo "$trname download was completed" | twidge update # Put "#" if you don't need this. # push update for pushover curl -s \ -F "token=$user-key" \ -F "user=$app-key" \ # -F "device=$device" \ # uncomment, if you want to send notification to a particular device. -F "title=Download Finished" \ -F "message=$trname download has completed." \ > /dev/null # The following codes works assuming One take advantage of gnome-power-manager by setting "black screen after 2/5/10/.. minitues ". # if monitor(Including laptop screen but EXCLUDING external monitor) is on, it will just force blank the screen, if not, it will shutdown/suspend or leave it as it is. # Modify it as per your requirement. STATUS=`xset -display $DISPLAY -q | grep 'Monitor'` #echo $STATUS if [ "$STATUS" == " Monitor is On" ] then notify-send "Downloads Complete" "turning off the screen now" xset dpms force off else notify-send "Downloads Complete" "$trname" # uncomment to shutdown the computer #dbus-send --session --type=method_call --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.RequestShutdown # uncomment to suspend (on ubuntu) #dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Suspend # uncomment to suspend (on Linux) (requires powermanagement-interface package) #pmi action suspend else echo "Torrent #$TORRENTID is not completed. Ignoring." fi
done A simple script.
Thanx to Khurshid Alam and user98677 I have written this script which is working fine. If display is on (you are working) it makes the Transmission to exit and sends a notification, if not computer is shutdown.
install
sudo apt-get install transmission-cli libnotify-binin Ubuntu 16.04
sudo apt install transmission-cli libnotify-bin- Transmission>Preferences>Remote>Check Allow remote Access make sure that HTTP Port is 9091 and only allow these IP Addresses is 127.0.0.1 (default).
- Copy paste the given script, save as say 'shutdown.sh' and make it executable.
- Transmission>Preferences>Downloading>Check 'Call script when download is completed', browse to the script.
System Settings>Power>screen brightness>Turn screen off when inactive for>select reasonable time.
#!/bin/bash sleep 300s DISPLAY=:0.0 STATUS=$(xset -display $DISPLAY -q | grep 'Monitor') STATE=$(transmission-remote 127.0.0.1:9091 -tall --info | grep "^ State:" | grep "Down" | wc --lines) if [ "$STATUS" == " Monitor is On" ] && [ "$STATE" == "0" ] then notify-send "Downloads Complete" "Exiting transmisssion now" pkill transmission elif [ "$STATE" == "0" ] then #in Ubuntu 16,04 shutdown -h now #in older versions use the following #dbus-send --session --type=method_call --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.RequestShutdown fi exit 0