Skip to content

Instantly share code, notes, and snippets.

@onedr0p
Last active October 21, 2023 19:04
Show Gist options
  • Save onedr0p/8fd8455f08f4781cad9e01a1d65bc34f to your computer and use it in GitHub Desktop.
Save onedr0p/8fd8455f08f4781cad9e01a1d65bc34f to your computer and use it in GitHub Desktop.
Transmission Garbage Collector
#!/bin/sh
#
# INFO
#
# This works if sonarr and radarr are set up to have a Category of sonarr and radarr respectively
# If you are using other Categories to save your automated downloads, update the script where you see:
# "radarr"|"sonarr")
# This script will not touch anything outside those Categories
# Set this file on a cron for every 5 minutes
# Using Docker? Make your cron something like this:
# /usr/bin/docker exec $(/usr/bin/docker ps | grep "linuxserver/transmission:latest" | awk '{print $1}') bash "/path/to/transmission-gc.sh"
# Set =~ to be insensitive
shopt -s nocasematch
TRANS_REMOTE_BIN=/usr/bin/transmission-remote
TRANS_HOST="127.0.0.1:9091"
# Amount of time (in seconds) after a torrent completes to delete them
RETENTION=2592000
# Delete torrents only when ratio is above
RATIO="1.5"
# Clean up torrents where trackers have torrent not registered
# filter list by * (which signifies a tracker error)
TORRENT_DEAD_LIST=($("${TRANS_REMOTE_BIN}" "${TRANS_HOST}" -l | sed -e '1d;$d;s/^ *//' | cut --only-delimited --delimiter=' ' --fields=1 | egrep '[0-9]+\*' | sed 's/\*$//'))
for torrent_id in "${TORRENT_DEAD_LIST[@]}"
do
# Get the torrents metadata
torrent_info=$("${TRANS_REMOTE_BIN}" "${TRANS_HOST}" --torrent "${torrent_id}" -i -it)
torrent_name=$(echo "${torrent_info}" | grep "Name: *" | sed 's/Name\:\s//i' | awk '{$1=$1};1')
torrent_path=$(echo "${torrent_info}" | grep "Location: *" | sed 's/Location\:\s//i' | awk '{$1=$1};1')
torrent_size=$(echo "${torrent_info}" | grep "Downloaded: *" | sed 's/Downloaded\:\s//i' | awk '{$1=$1};1')
torrent_label=$(basename "${torrent_path}")
case "${torrent_label}" in
"radarr"|"sonarr")
torrent_error=$(echo "${torrent_info}" | grep "Got an error" | cut -d \" -f2)
if [[ "${torrent_error}" =~ "unregistered" ]] || [[ "${torrent_error}" =~ "not registered" ]]; then
# Delete torrent
"${TRANS_REMOTE_BIN}" "${TRANS_HOST}" --torrent "${torrent_id}" --remove-and-delete > /dev/null
fi
esac
done
# Clean up torrent where ratio is > ${RATIO} or seeding time > ${RETENTION} seconds
# do not filter the list, get all the torrents
TORRENT_ALL_LIST=($("${TRANS_REMOTE_BIN}" "${TRANS_HOST}" -l | sed -e '1d;$d;s/^ *//' | cut --only-delimited --delimiter=' ' --fields=1))
for torrent_id in "${TORRENT_ALL_LIST[@]}"
do
# Get the torrents metadata
torrent_info=$("${TRANS_REMOTE_BIN}" "${TRANS_HOST}" --torrent "${torrent_id}" -i -it)
torrent_name=$(echo "${torrent_info}" | grep "Name: *" | sed 's/Name\:\s//i' | awk '{$1=$1};1')
torrent_path=$(echo "${torrent_info}" | grep "Location: *" | sed 's/Location\:\s//i' | awk '{$1=$1};1')
torrent_size=$(echo "${torrent_info}" | grep "Downloaded: *" | sed 's/Downloaded\:\s//i' | awk '{$1=$1};1')
torrent_label=$(basename "${torrent_path}")
torrent_seeding_seconds=$(echo "${torrent_info}" | grep "Seeding Time: *" | awk -F"[()]" '{print $2}' | sed 's/\sseconds//i')
torrent_ratio=$(echo "${torrent_info}" | grep "Ratio: *" | sed 's/Ratio\:\s//i' | awk '{$1=$1};1')
# Debug
# echo "${torrent_id} - ${torrent_ratio} - ${torrent_seeding_seconds} - ${torrent_label} - ${torrent_name}"
case "${torrent_label}" in
"radarr"|"sonarr")
# Torrents without a ratio have "None" instead of "0.0" let's fix that
if [[ "${torrent_ratio}" =~ "None" ]]; then
torrent_ratio="0.0"
fi
# delete torrents greater than ${TTL_SECONDS}
if [[ "${torrent_seeding_seconds}" -gt "${RETENTION}" ]]; then
"${TRANS_REMOTE_BIN}" "${TRANS_HOST}" --torrent "${torrent_id}" --remove-and-delete > /dev/null
fi
# delete torrents greater than ${RATIO}
if (( $(echo "${torrent_ratio} ${RATIO}" | awk '{print ($1 > $2)}') )); then
"${TRANS_REMOTE_BIN}" "${TRANS_HOST}" --torrent "${torrent_id}" --remove-and-delete > /dev/null
fi
esac
done
@bryangauvin
Copy link

Can someone please help me out. Im trying to set this up on windows. I can't find any documentation on windows setup. What do I need to modify to get this working on windows

@cat24max
Copy link

Sadly this script is broken :(

@onedr0p
Copy link
Author

onedr0p commented Oct 27, 2019

What is broken? It works for me.

@cat24max
Copy link

I tried to use it a few hours ago and got a bunch of errors. On my phone right now, but I think it was line 17 and 30 (missing bracket). Didn‘t have time to look further, though.

@onedr0p
Copy link
Author

onedr0p commented Oct 27, 2019

I have ran the script thru shellchecker and it appears to be valid, so unless you made any changes the problem is on your end.

@jmrushing
Copy link

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

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

@marc0janssen
Copy link

The transmission-daemon resets the seeding time . So you will never get to the "RETENTION" seeding time.

This version of the script fixes that

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