linuxbashtorrentdeluge

Trying to get script working for Disk check and Deluge


I managed to get this script working earlier but then it stopped working and now I always get this error and the logs don't show any information the .log file is empty

My script was working fine until I changed { df -k ${fileSystem}|tail -n1 } to { quota -u |tail -n1 } because it shows the correct usage assigned to me instead of the entire seedbox

user@hera:~/scripts$ df -k /home20/<user>|tail -n1
/dev/sdu1      15616058976 3311158640 12303321368  22% /home20
user@hera:~/scripts$ quota -u <user>|tail -n1
      /dev/sdu1 1629501728  1953497088 1953497088            4344       0       0
tail: deluge-disk-check.log: file truncated

[empty]
./deluge-disk-check.sh: line 23: let: freeSpacePct=100*/: syntax error: operand expected (error token is "/")

my script

#!/bin/bash

exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>/home/<user>/scripts/deluge-disk-check.log 2>&1

# Adjust these parameters to your system
fileSystem="/home/<user>"     # Filesystem you want to monitor
minFreeSpace1="78"   # Seedbox Free space 1st threshold percentage
minFreeSpace2="77"   # Seedbox Free space 2nd threshold percentage
checkInterval="3600"     # Interval between checks in seconds
SERVICE='deluged'

  while (:); do
    # Get the output of df -k and put in a variable for parsing
       dfOutPut=$(df -k ${fileSystem}|tail -n1)

    # Exctract the fields containing total and available 1K-blocks
       totalBlocksKb=$(echo "${dfOutPut}" | awk '{print $2}')
       availableBlocksKb=$(echo "${dfOutPut}" | awk '{print $4}')

    # Calculate percentage of free space
    let freeSpacePct=100\*${availableBlocksKb}/${totalBlocksKb}

    # Check if free space percentage is below threshold value
    if [ "${freeSpacePct}" -lt "${minFreeSpace1}" ]; then
       date +'%Y-%m-%d %H:%M:%S'
       echo "You only have ${freeSpacePct}% free space on seedbox"
   # Check whether the instance of thread exists:
    if ps ax | grep -v grep | grep $SERVICE > /dev/null
  then
       echo "Deluge is running, Is there free space on device?"
       pkill deluge
       echo -e "No, Trying to stop Deluge...\nDeluge stopped, exiting"
  else
   if [ "${freeSpacePct}" -lt "${minFreeSpace2}" ]; then
       echo "refreshing data..."
       echo "Deluge is not running, Is there free space on device?"
       echo "Yes, Threshold value is now ${minFreeSpace2}%"
       echo "Trying to restart Deluge ..." && app-deluge restart
       echo "Deluge is running, exiting"
    sleep ${checkInterval}
    fi
  fi
fi
done


Solution

  • On line 23:

    let freeSpacePct=100\*${availableBlocksKb}/${totalBlocksKb}
    

    For arithmetic evaluations, use $(( ... ))) like this:

    let freeSpacePct=$(( 100 * ${availableBlocksKb} / ${totalBlocksKb} ))
    

    or more simply (without ${...} around variable names):

    let freeSpacePct=$(( 100 * availableBlocksKb / totalBlocksKb ))