Looking for help setting up an unrar script with Transmission

Stingray88

Dabbler
Joined
Aug 19, 2015
Messages
38
Hey all,

I'm running FreeNAS 11.2 U3, and have no issues using SABnzbd, Radarr, Sonarr together for automatic Usenet downloads, all working fine there. I've also got Jackett and Transmission setup for automatic Torrent downloads from Sonarr. However the one piece that I've been working on for months without any luck is the automatic extraction of RAR files. I've followed a number of different guides on these forums, Reddit, Transmission forums, and random blogs, but have never been successful in the end.

My latest effort was following this guide as well as this guide. I've confirmed unrar is installed in the Transmission jail and works properly. I've created a script that is executable and owned by the media user/group that Transmission runs as. The script contains this -

#!/bin/bash
#A simple script to extract a rar file inside a directory downloaded by Transmission.
#It uses environment variables passed by the transmission client to find and extract any rar files from a downloaded torrent into the folder they were found in.
find /$TR_TORRENT_DIR/$TR_TORRENT_NAME -name "*.rar" -execdir unrar e -o- "{}" \;


And I've made sure to set these flags within settings.json using the instructions here.

"script-torrent-done-enabled": true,
"script-torrent-done-filename": "/path/to/script.sh",


Unfortunately that's as far as I've gotten. I can't tell if the script is running or not after a Torrent finishes, and I can't tell if the script would work properly even if it did run. What are the next steps I should take to make this work?
 

Heracles

Wizard
Joined
Feb 2, 2018
Messages
1,401
Hey Stingray,

Here is a first debug for you. It will tell us if the script is running or not. If it is not, you will know better what you have to diagnose and fix. If it does run, we will re-use this to debug and fix whatever else is broken.

Instead of trying to jump to the end and open your archive, just put a flag in the filesystem. In your bash script, as a first command, do something like :

echo "Debug flag No1" >> /tmp/flags.txt
end

That will just print the string "Debug flag No1" and add it at the end of the file /tmp/flags.txt. The script will then stop.

After you called your script, go in the filesystem and look for that /tmp/flags.txt file and see if it contains your flag.

If it does not, debug this first until you get your flag saved in the file.

Once you got the flag written in the file, improve your flag with a time stamp :

echo "Debug flag No1" >> /tmp/flags.txt
date >> /tmp/flags.txt
end

With that timestamp, you can distinguish between multiple flags.

Once your flags are working, you can keeo working with that. Do somehting like "which command >> /tmp/flags.txt" to be sure the script can find the command you are looking for and tell you the path from which it will run it. Doing step-by-step checks like that should point you to whatever your problem is.

Hope these easy debugging tricks will help you fix your own script,
 

Stingray88

Dabbler
Joined
Aug 19, 2015
Messages
38
Hey Stingray,

Here is a first debug for you. It will tell us if the script is running or not. If it is not, you will know better what you have to diagnose and fix. If it does run, we will re-use this to debug and fix whatever else is broken.

Instead of trying to jump to the end and open your archive, just put a flag in the filesystem. In your bash script, as a first command, do something like :

echo "Debug flag No1" >> /tmp/flags.txt
end

That will just print the string "Debug flag No1" and add it at the end of the file /tmp/flags.txt. The script will then stop.

After you called your script, go in the filesystem and look for that /tmp/flags.txt file and see if it contains your flag.

If it does not, debug this first until you get your flag saved in the file.

Once you got the flag written in the file, improve your flag with a time stamp :

echo "Debug flag No1" >> /tmp/flags.txt
date >> /tmp/flags.txt
end

With that timestamp, you can distinguish between multiple flags.

Once your flags are working, you can keeo working with that. Do somehting like "which command >> /tmp/flags.txt" to be sure the script can find the command you are looking for and tell you the path from which it will run it. Doing step-by-step checks like that should point you to whatever your problem is.

Hope these easy debugging tricks will help you fix your own script,

Thanks Heracles! This is a very valuable trick to remember.

So I've commented out the rest of my script, and inserted the debug flag No1 and date flags at the end. When I run the script in terminal, it works as expected and I see these flags within /tmp/flags.txt. With that I've confirmed the script is executable, and that it will execute from both the main FreeNAS shell, and within the Transmission jail shell.

Unfortunately I've also confirmed that Transmission is not executing the script after a torrent has finished downloading, as I'm not seeing new flags popping up in flags.txt.

I've double-checked that the Transmission settings.json file contains the flags to run a script after a download finishes, and the path to where the script lives. The script lives in the same area the Transmission downloads media to, so it should have read/write permission.

Any idea where I should go from here to see why Transmission isn't running the script?
 

Heracles

Wizard
Joined
Feb 2, 2018
Messages
1,401
Hi again,

Good that you managed to progress to this point. So now, why that script refuses to run...

Now, we know that the script can run from an interactive shell. So next step is to run it from a non-interactive shell. An interactive shell comes with variables, path and resources that a non-interactive shell will miss. These elements can easily make the difference between running and not running. For that, your next step is to get that script to run from CRON.

So first thing is to get familiar with CRON and how to program a cron job. For that, you simply need to edit your crontab and add an entry pointing to your script and starting it next minutes. Use the command "date" to see exactly what time it is, wait for the system to turn a new minutes (ex: xx:12 minutes), edit your crontab to add your script to run at every xx:13 minute and wait. Right after xx:13, you go check your flags. Did your script worked ?

You can try to run it from a root's crontab as well as from the specific UID you need your final script to run.

Once you got your script running from cron and saving your flags correctly, you can keep pushing a step further toward your end goal.

Keep working on it, you will get it where you want :smile:
 

Stingray88

Dabbler
Joined
Aug 19, 2015
Messages
38
Hi again,

Good that you managed to progress to this point. So now, why that script refuses to run...

Now, we know that the script can run from an interactive shell. So next step is to run it from a non-interactive shell. An interactive shell comes with variables, path and resources that a non-interactive shell will miss. These elements can easily make the difference between running and not running. For that, your next step is to get that script to run from CRON.

So first thing is to get familiar with CRON and how to program a cron job. For that, you simply need to edit your crontab and add an entry pointing to your script and starting it next minutes. Use the command "date" to see exactly what time it is, wait for the system to turn a new minutes (ex: xx:12 minutes), edit your crontab to add your script to run at every xx:13 minute and wait. Right after xx:13, you go check your flags. Did your script worked ?

You can try to run it from a root's crontab as well as from the specific UID you need your final script to run.

Once you got your script running from cron and saving your flags correctly, you can keep pushing a step further toward your end goal.

Keep working on it, you will get it where you want :)

Thanks again Heracles, now I've learned another useful skill, how to use CRON.

The script prints my flags to flags.txt when run by CRON under root, my personal user, as well as media which is the user Transmission runs as.

What should my next steps be?
 

Heracles

Wizard
Joined
Feb 2, 2018
Messages
1,401
Hi Stingray,

Great that you passed another mark. Now you will have to get your script to run, called by cron but from inside the jail instead of outside. Also, remember that the flags will be saved in the /tmp relative to the jail and not the one at the root of the filesystem. It will probably be somewhere like /mnt/pool/dataset/jail/tmp/flags.txt instead of just /tmp/flags.txt.

So either you put a cron daemon inside your jail or you use the main cron and start a jailed shell to call your script.

Also, know that the first flag using echo serves two purposes. The ECHO command itself is built in the bash shell. As such, from the moment the shell is running, it does not need to look anywhere to fund that command (as opposed to the date command). Another thing to check is to be sure that there is a /tmp inside your jail.

Hope this will give you more ideas about what to test and how to test it,
 

Stingray88

Dabbler
Joined
Aug 19, 2015
Messages
38
start a jailed shell to call your script.

Hey Heracles, unfortunately I'm not sure if I'm understanding what you mean by this. Do you mean I should do this?

iocage exec transmission /path/to/script/script.sh

Unfortunately when I try this, I get the response that the script is not found. Same story if I use the absolute path in FreeNAS, as well as the path with respect to the jail.

Am I missing something here?
 

Heracles

Wizard
Joined
Feb 2, 2018
Messages
1,401
Hi,

You said that you managed to get the script to run from the Terminal jail shell. So this is what you need to do but not from an interactive shell typing it yourself on the keyboard. You can either go manually in the interactive shell to install Cron in that jail and have the script run from that cron, or find the way to start your script from a shell called by the cron running in the core instance instead of the interactive shell you used so far.

Unfortunately, I do not use iocage myself, so there is not much I can tell you about how to use it. I run my VM in my ESXi server and I have a Docker host in FreeNAS. In that one, Docker and tools deployed in the containers are what I use for batch jobs.

Good luck debugging your script,
 

PD_ANZ

Cadet
Joined
Aug 16, 2017
Messages
8
Not sure if you ever figured this out but I will post in case someone else is looking.

The jail doesn't have BASH by default. Only SH.

So the top of your script should be

Code:
#!/bin/sh


So the full script would be:

Code:
#!/bin/sh
#A simple script to extract a rar file inside a directory downloaded by Transmission.
#It uses environment variables passed by the transmission client to find and extract any rar files from a downloaded torrent into the folder they were found in.
find /$TR_TORRENT_DIR/$TR_TORRENT_NAME -name "*.rar" -execdir unrar e -o- "{}" \;
 

Naesstrom

Contributor
Joined
Jul 10, 2012
Messages
108
Anyone got this script working... I've changed the top to /sh as noted above, made sure it is executable and added the stuff to the settings.json but it still doesn't seem to unpack the files. It should unrar them to the same location as the rar files right?
 

Stingray88

Dabbler
Joined
Aug 19, 2015
Messages
38
Unfortunately no, I never got this even close to working. Eventually I just gave up.

Like a year later I noticed qBittorent was an option in the community supported plugin list... Installed that to check it out and saw that it has the ability to run scripted commands built right into the GUI, and then I found this https://www.truenas.com/community/t...-extract-unrar-upon-torrent-completion.56013/

This works perfectly for me and took less than 5 minutes to setup. I ditched transmission after that and never looked back. So much simpler than writing a script yourself...
 

Naesstrom

Contributor
Joined
Jul 10, 2012
Messages
108
Unfortunately no, I never got this even close to working. Eventually I just gave up.

Like a year later I noticed qBittorent was an option in the community supported plugin list... Installed that to check it out and saw that it has the ability to run scripted commands built right into the GUI, and then I found this https://www.truenas.com/community/t...-extract-unrar-upon-torrent-completion.56013/

This works perfectly for me and took less than 5 minutes to setup. I ditched transmission after that and never looked back. So much simpler than writing a script yourself...

Thanks, Qbit running, unraring and integrated into radarr and sonarr without any hassle! Just using transmission now for any rare manual episodes!
 
Top