尝试让脚本用于磁盘检查和 Deluge

Trying to get script working for Disk check and Deluge

我设法让这个脚本早些时候工作,但后来它停止工作,现在我总是收到这个错误,日志不显示任何信息,.log 文件是空的

我的脚本工作正常,直到我将 { df -k ${fileSystem}|tail -n1 } 更改为 { quota -u |tail -n1 } 因为它显示了分配给我的正确用法而不是整个种子箱

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 "/")

我的脚本

#!/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 }')
       availableBlocksKb=$(echo "${dfOutPut}" | awk '{print }')

    # 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

第 23 行:

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

对于算术计算,使用 $(( ... ))) 像这样:

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

或更简单(变量名称周围没有 ${...}):

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