Skip to content

Instantly share code, notes, and snippets.

@kolya182
Forked from brimur/checkDisk.sh
Created November 27, 2019 01:12
Show Gist options
  • Save kolya182/a59476896a5f8ecee5010bb87e4bfcff to your computer and use it in GitHub Desktop.
Save kolya182/a59476896a5f8ecee5010bb87e4bfcff to your computer and use it in GitHub Desktop.
Cron script to control
# 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"
limit="95"
#End customise
percent=$(df -h | grep $partition | awk '{ print $5 }' | sed 's/%//g')
echo Disk is "$percent" % full
if ((percent > limit))
then
echo Disk is "$percent" % full. Stopping Torrents
transmission-remote -t all -S
fi
space=$(df -h | grep "$partition" | awk '{ print $4 }' | sed 's/G//g')
space=$(echo "$space*1000" | bc)
echo Free space is "$space" MBs
for tor in $(transmission-remote -l | grep -Ev "Finished|Stopped|ID|Sum:" | sed 's/%//g' | awk '{print $1}'); do
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}')
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)" | bc)
if ((left > space))
then
echo Torrent "$tor" 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
else
space=$(echo "$space - $left" | bc)
echo Torrent "$tor" needs another "$left" MBs to finish so available space is now "$space" MBs
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
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}')
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)" | bc)
if ((space > left))
then
echo Starting "$tor" because "$left" MBs remaining and that is less than "$space" MBs
transmission-remote -n 'transmission:transmission' -t "$tor" -s
space="$(echo "$space" - "$left" | bc)"
else
echo Skipping torrent "$tor" as it requires "$left" MBs to complete but there is only "$space" MBs left
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment