Skip to content

Instantly share code, notes, and snippets.

@pawelszydlo
Last active June 10, 2024 14:37
Show Gist options
  • Save pawelszydlo/e2e1fc424f2c9d306f3a to your computer and use it in GitHub Desktop.
Save pawelszydlo/e2e1fc424f2c9d306f3a to your computer and use it in GitHub Desktop.
Script to clear finished torrents from transmission-daemon
#!/bin/bash
# Clears finished downloads from Transmission.
# Version: 1.1
#
# Newest version can always be found at:
# https://gist.github.com/pawelszydlo/e2e1fc424f2c9d306f3a
#
# Server string is resolved in this order:
# 1. TRANSMISSION_SERVER environment variable
# 2. Parameters passed to this script
# 3. Hardcoded string in this script (see below).
# Server string: "host:port --auth username:password"
SERVER="host:port --auth user:pass"
# Which torrent states should be removed at 100% progress.
DONE_STATES=("Seeding" "Stopped" "Finished" "Idle")
# Get the final server string to use.
if [[ -n "$TRANSMISSION_SERVER" ]]; then
echo -n "Using server string from the environment: "
SERVER="$TRANSMISSION_SERVER"
elif [[ "$#" -gt 0 ]]; then
echo -n "Using server string passed through parameters: "
SERVER="$*"
else
echo -n "Using hardcoded server string: "
fi
echo "${SERVER: : 10}(...)" # Truncate to not print auth.
# Use transmission-remote to get the torrent list from transmission-remote.
TORRENT_LIST=$(transmission-remote $SERVER --list | sed -e '1d' -e '$d' | awk '{print $1}' | sed -e 's/[^0-9]*//g')
# Iterate through the torrents.
for TORRENT_ID in $TORRENT_LIST
do
INFO=$(transmission-remote $SERVER --torrent "$TORRENT_ID" --info)
echo -e "Processing #$TORRENT_ID: \"$(echo "$INFO" | sed -n 's/.*Name: \(.*\)/\1/p')\"..."
# To see the full torrent info, uncomment the following line.
# echo "$INFO"
PROGRESS=$(echo "$INFO" | sed -n 's/.*Percent Done: \(.*\)%.*/\1/p')
STATE=$(echo "$INFO" | sed -n 's/.*State: \(.*\)/\1/p')
# If the torrent is 100% done and the state is one of the done states.
if [[ "$PROGRESS" == "100" ]] && [[ "${DONE_STATES[@]}" =~ "$STATE" ]]; then
echo "Torrent #$TORRENT_ID is done. Removing torrent from list."
transmission-remote $SERVER --torrent "$TORRENT_ID" --remove
else
echo "Torrent #$TORRENT_ID is $PROGRESS% done with state \"$STATE\". Ignoring."
fi
done
@yurividal
Copy link

Added this to my crontab and clear torrents automatically every 5 days. (So that i can be fair and seed for some time)

@onedr0p
Copy link

onedr0p commented May 9, 2018

@packet9
Copy link

packet9 commented May 10, 2018

@pawelszydlo Great job ! working well on my pi, got to automatize with cron. Thanks !

@norcalli
Copy link

norcalli commented Apr 1, 2019

Here's my oneliner that uses awk:

transmission-remote -l | awk '$2 == "100%"{ system("transmission-remote -t " $1 " --remove") }'

@FredrikM97
Copy link

FredrikM97 commented Sep 11, 2019

Added some more features to the code a bit in order to delete based on download/seeding time and deal with dead torrents too (check link below). Thank you very much for your contribution!
https://github.com/FredrikM97/Transmission-DH

@yuljk
Copy link

yuljk commented Sep 19, 2019

Hi guys - I'm looking for the same functionality on Windows. Has anyone done this using Powershell?

@FredrikM97
Copy link

Cant you run the bssh scripts in CMD?

@J7mbo
Copy link

J7mbo commented Sep 25, 2019

You can do this on windows. Install debian.

Then in powershell you have to double escape quotes if you have any inside:

bash -c "echo ""hi"""

Then if you want to run powershell from the command line, you'll have even more fun with double escapes!

powershell -command "bash -c ""echo ""hello world""""

Enjoy.

@cybertramp
Copy link

It's comfortable thanks a lot!

@jmrushing
Copy link

jmrushing commented Dec 22, 2019

FYI, for anyone that lands here from google, but is looking for a solution for dockerized transmission - such as Haugene's awesome transmission/openvpn docker - Here's a solution that worked for me...

First create the script itself that will live inside the container and name it something recognizable. I used remove_finished_torrents. Don't worry about the environment variables yet.

#!/bin/bash
transmission-remote --auth $tmpUser:$tmpPass -l | awk '$2 == "100%" && (( $9 == "Stopped" || $9 == "Finished" )){ system("transmission-remote --auth $tmpUser:$tmpPass -t " $1 " --remove") }'

Then copy that file over to the container. It's probably best to copy it somewhere that will get mounted every time the container restarts and won't get wiped by any container updates from the maintainers. I have a local storage volume that gets mounted to /shared at container startup. (You can find [CONTAINER_ID] by running docker ps and looking for the container you recognize as your transmission client)

User@HostMachine:~$ docker cp ~/remove_finished_torrents [CONTAINER_ID]:/shared/

Next, jump into your container and make sure the remove_finished_torrents script has the correct ownership & permissions. Because of how mine is setup, I made the script owned & only executable by root.

User@HostMachine:~$ docker exec -it [CONTAINER_ID] /bin/bash
root@a0b1c2d3e4f5:/# chown root:root /shared/remove_finished_torrents
root@a0b1c2d3e4f5:/# chmod 774 /shared/remove_finished_torrents
root@a0b1c2d3e4f5:/# exit

Finally, outside the container, on the host, make another short script that cron will call. For my purposes, I have a cleanup script that runs daily from my cron. It looks something like this:

#!/bin/bash
HAUGENE_ID=`/usr/bin/docker ps | grep "haugene" | awk '{print $1}'`
docker exec -e tmpUser=$RPC_U -e tmpPass=$RPC_P $HAUGENE_ID /shared/remove_finished_torrents

Don't forget to make it executable:
User@HostMachine:~$ sudo chmod +x cleanup

If you're wondering about tmpUser, tmpPass, and the RPC variables: I have my transmission rpc login/pass set through /etc/environment. What I'm doing in this last script is taking the variables already setup - $RPC_U and $RPC_P - and passing them to temporary variables inside the transmission docker container - tmpUser and tmpPass. You can just as easily alter this by doing -e tmpUser="MyActualUsername" -e tmpPass="MyActualPassword" or omit the 2 environment flags entirely from this script and hardcode your user/pass into the original remove_finished_torrents script by changing the 2 instances of $tmpUser:$tmpPass to MyActualUsername:MyActualPassword.

I would definitely recommend leaving in the HAUGENE_ID line (change the variable name to whatever you like) just to make the next command easier to read. Of course, if you use a different docker image instead of haugene/transmission-openvpn, you'll need to change "haugene" to something maybe "transmission" or "linuxserver/transmission" if that's what you use so that grep & awk can correctly match your container id. Remember your container id will change ever time you restart it, that's why this line is so crucial.

My cleanup script just sits in the home dir and user's crontab references it:

User@HostMachine:~$ crontab -l
0 4 * * * /home/User/cleanup>/home/User/clean.log

It runs at 4am local time everyday and redirects STOUT from the script to a clean.log file.

This might seem a little convoluted, having one script on the docker container and one script called from the host machine's cron to trigger the docker script, but it works great for how I have my server setup with usernames & passwords living in environment variables outside the container. The magic of finding the finished torrents and removing them is really in that one line from the first script, so that could be easily adapted to someone else's situation.

I worked out this solution using OP's script, as well as this one, and this comment

@ToXinE
Copy link

ToXinE commented Feb 6, 2020

FYI, for anyone that lands here from google, but is looking for a solution for dockerized transmission - such as Haugene's awesome transmission/openvpn docker - Here's a solution that worked for me...

Thanks a lot ! That was exactly what is was looking for !

@kzaoaai
Copy link

kzaoaai commented May 23, 2020

FYI, for anyone that lands here from google, but is looking for a solution for dockerized transmission - such as Haugene's awesome transmission/openvpn docker - Here's a solution that worked for me...

Thank you, this is perfect.

@pawelszydlo
Copy link
Author

pawelszydlo commented May 24, 2020

I am very happy that you find my script useful. I have updated it a bit.
If you find any issues or have any wishes about the functionality you would like to see added, please let me know!

@refresh-8104
Copy link

refresh-8104 commented Jun 3, 2020

for me.. it's not working properly.
i'm using Transmission 2.94 (d8e60ee44f) on synology NAS.

error message
line 33: transmission-remote: command not found

@pawelszydlo
Copy link
Author

error message
line 33: transmission-remote: command not found

This script only automates the "transmission-remote", which you need to have. On linux (Debian at least) this comes from the "transmission-cli" package. I tried a quick google search about how to get it on Synology NAS, but without luck.

@dwalintukan
Copy link

It comes packaged in the Transmission app if you downloaded through the Package Manager. Can find it in /usr/local/transmission/bin

@03397
Copy link

03397 commented Oct 16, 2020

Does this script delete only the finished torrents or it deletes the downloaded data also?

@pawelszydlo
Copy link
Author

Does this script delete only the finished torrents or it deletes the downloaded data also?

It only removes the transmission download. It does not touch the files.

@FredrikM97
Copy link

Does this script delete only the finished torrents or it deletes the downloaded data also?

If you modify "transmission-remote $SERVER --torrent "$TORRENT_ID" --remove" you can change so it also removes the files :)

@treecrazy
Copy link

treecrazy commented Oct 25, 2020

Running this script manually from the terminal, I get an error:

transmission_remove_finished.sh: 18: transmission_remove_finished.sh: Syntax error: "(" unexpected<

Any idea what I may be doing wrong?

@jcrincon
Copy link

jcrincon commented Nov 21, 2020

#!/bin/bash
HAUGENE_ID=`/usr/bin/docker ps | grep "haugene" | awk '{print $1}'`
docker exec -e tmpUser=$RPC_U -e tmpPass=$RPC_P $HAUGENE_ID /shared/remove_finished

Most likely it was:

/shared/remove_finished_torrents

The script also applies to Transmission (vanilla):

#!/bin/bash
TRANSMISSION_ID=`/usr/bin/docker ps | grep "transmission" | awk '{print $1}'`
docker exec -e tmpUser=$RPC_U -e tmpPass=$RPC_P $TRANSMISSION_ID /shared/remove_finished_torrents

PS. For me it was crontab -e

@jmrushing
Copy link

Most likely it was:

/shared/remove_finished_torrents

Good catch. This was a typo. I just corrected it in the original comment.

The script also applies to Transmission (vanilla):

Glad it works for you. I've had this script running on my setup for roughly 2 years without issue.

PS. For me it was crontab -e

Ah yes, I copied the terminal output after the fact (crontab -l) rather than document how to actually edit it. 🙃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment