Last active
October 25, 2021 07:01
-
-
Save brimur/c24f79182b947ac36d48a2b5dfef02b1 to your computer and use it in GitHub Desktop.
Cron script to control
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Name: checkDisk.sh | |
# Created by: Brimur | |
# Created: Nov 26th 2019 | |
# This script is used to control Transmission jobs based on available disk space. | |
# | |
# This should be run as a cronjob (every minute) on the server/computer running Transmission | |
# | |
# If you use a download manager tell it to add jobs as PAUSED | |
# | |
# - If disk is nearly full (limit) then stop all jobs | |
# | |
# - Gets list of running jobs | |
# --- Checks if there is enough space to complete all runnning jobs | |
# --- Stops one or more jobs if there is not enough space to complete | |
# - Gets list of stopped jobs | |
# --- Check size of job and starts it if there is enough space to complete the job | |
# | |
#!/bin/bash | |
# Customise here... | |
partition="/dev/mapper/vg-lv_root" | |
threshold="95" | |
#End customise | |
percent=$(df -h | grep $partition | awk '{ print $5 }' | sed 's/%//g') | |
echo Disk is "$percent" % full | |
if ((percent > threshold)) | |
then | |
echo Disk is "$percent" % full. Stopping Torrents | |
transmission-remote -n 'transmission:transmission' -t all -S | |
else | |
space=$(df -h | grep "$partition" | awk '{ print $4 }' | sed 's/G//g') | |
space=$(echo "($space*1000 +0.5)/1" | bc) | |
echo Free space is "$space" MBs | |
eta=0 | |
for tor in $(transmission-remote -l | grep -Ev "Finished|Stopped|ID|Sum:" | sed 's/%//g' | awk '{print $1}'); do | |
name=$(transmission-remote -t "$tor" -i | grep "Name:" | awk '{print $2}' | cut -d'.' -f1,2,3) | |
total=$(transmission-remote -t "$tor" -i | grep "Total size" | sed 's/(//g' | awk '{print $5}') | |
totalType=$(transmission-remote -t "$tor" -i | grep "Total size" | sed 's/(//g' | awk '{print $6}') | |
have=$(transmission-remote -t "$tor" -i | grep "Have" | sed 's/(//g' | sed 's/)//g' | awk '{print $4}') | |
haveType=$(transmission-remote -t "$tor" -i | grep "Have" | sed 's/(//g' | sed 's/)//g' | awk '{print $5}') | |
dTime=$(transmission-remote -t "$tor" -i | grep "Downloading Time" | sed 's/.*(\(.*\))/\1/' | awk '{print $1}') | |
eta=$(transmission-remote -t "$tor" -i | grep "ETA:" | sed 's/.*(\(.*\))/\1/' | awk '{print $1}') | |
#echo Looking at "$name" | |
if [ "$total" == "wanted)" ];then | |
total=0 | |
fi | |
if [ "$have" == "verified" ]; then | |
have=0 | |
fi | |
if [ "$totalType" == "GB" ]; then | |
total=$(echo "($total + 0.5)/1" | bc) | |
total=$(echo "$total*1000" | bc) | |
fi | |
if [ "$haveType" == "GB" ]; then | |
have=$(echo "($have + 0.5)/1" | bc) | |
have=$(echo "$have*1000" | bc) | |
fi | |
left=$(echo "($total - $have +0.5)/1" | bc) | |
#echo Tor: $tor - Left $left - Space $space - ETA $eta - Downloading $dTime | |
if (( left > space && eta>1800 && dTime>600 )) | |
then | |
echo "$name" is running but needs "$left" MBs to complete and there is only "$space" left so stopping it | |
transmission-remote -n 'transmission:transmission' -t "$tor" -S | |
#elif (( eta > 86400 )) | |
#then | |
# echo "$name" ETA is very high so just going to ignore it | |
#elif (( eta == 0 )) | |
#then | |
# echo "$name" ETA is very high so just going to ignore it | |
else | |
if(( left < space )) | |
then | |
space=$(echo "($space - $left +0.5)/1" | bc) | |
echo "$name" needs another "$left" MBs to finish so available space is now "$space" MBs | |
else | |
oldSpace=$space | |
space=$(echo "($space - $left +0.5)/1" | bc) | |
echo "$name" needs another "$left" MBs but the ETA of "$eta" suggests it will finish before disk space runs out. | |
echo ... temporarily setting disk space to "$space" | |
fi | |
fi | |
done | |
stop=$(transmission-remote -l | grep -e Unknown | grep -e Stopped -c) | |
echo There are "$stop" stopped torrents and "$space" MBs free space | |
for tor in $(transmission-remote -l | grep -e "Stopped" | awk '{print $1}'); do | |
name=$(transmission-remote -t "$tor" -i | grep "Name:" | awk '{print $2}' | cut -d'.' -f1,2,3) | |
total=$(transmission-remote -t "$tor" -i | grep "Total size" | sed 's/(//g' | awk '{print $5}') | |
totalType=$(transmission-remote -t "$tor" -i | grep "Total size" | sed 's/(//g' | awk '{print $6}') | |
have=$(transmission-remote -t "$tor" -i | grep "Have" | sed 's/(//g' | sed 's/)//g' | awk '{print $4}') | |
haveType=$(transmission-remote -t "$tor" -i | grep "Have" | sed 's/(//g' | sed 's/)//g' | awk '{print $5}') | |
dTime=$(transmission-remote -t "$tor" -i | grep "Downloading Time" | sed 's/.*(\(.*\))/\1/' | awk '{print $1}') | |
if [ "$total" == "wanted)" ];then | |
total=0 | |
fi | |
if [ "$have" == "verified" ]; then | |
have=0 | |
fi | |
if [ "$totalType" == "GB" ]; then | |
total=$(echo "($total + 0.5)/1" | bc) | |
total=$(echo "$total*1000" | bc) | |
fi | |
if [ "$haveType" == "GB" ]; then | |
have=$(echo "($have + 0.5)/1" | bc) | |
have=$(echo "$have*1000" | bc) | |
fi | |
if [ "$have" == "None" ] || [ "$have" == "verified" ]; then | |
have=0 | |
fi | |
left=$(echo "($total - $have +0.5)/1" | bc) | |
if ((space > left)) | |
then | |
echo Starting "$name" because "$left" MBs remaining and that is less than "$space" MBs | |
transmission-remote -n 'transmission:transmission' -t "$tor" -s | |
space=$(echo "($space - $left +0.5)/1" | bc) | |
elif ((dTime < 600)) | |
then | |
echo Starting "$name" because its new and I want to see how fast and big is it | |
transmission-remote -n 'transmission:transmission' -t "$tor" -s | |
else | |
echo Skipping "$name" as it requires "$left" MBs to complete but there is only "$space" MBs left | |
fi | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment