Bash 腳本無法與 crontab 一起使用

Bash 腳本無法與 crontab 一起使用

我擁有一個運行 raspbian jessie 的樹莓派。我使用帶有 Motioneye 的樹莓派作為系統監控。 Motioneye 將檔案記錄到 SD 卡,但我想在磁碟使用量滿時刪除資料夾 /var/lib/motioneye 中的舊檔案。 Motioneye 將電影檔案記錄在 /var/lib/motioneye 中,並包含每天的子目錄,例如 /var/lib/motioneye/2016-02-13/files_in_order。 Motioneye 使用此權限記錄文件 drwxr-xr-x 2 root root 4096 Feb 13 17:03 2016-02-13 所以我無法刪除具有使用者存取權限的文件,我需要處於 root 狀態。我可以毫無問題地運行腳本

sudo bash ./deleteoldfiles.sh

但是當我在 crontab 中輸入它時,腳本不起作用,我嘗試輸入路徑但沒有成功。我在互聯網上找到了這個腳本,進行了一些修改,以便它可以接受文件夾而不是安裝驅動程序,我在腳本文件夾鏈接和最大使用量中寫入。我的腳本如下圖所示:

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/pi/
############################################################################### 
# Author            :  Louwrentius
# Contact           : [email protected]
# Initial release   : August 2011
# Licence           : Simplified BSD License
############################################################################### 

VERSION=1.01

#
# Mounted volume to be monitored.
#
MOUNT="/var/lib/motioneye"
#
# Maximum threshold of volume used as an integer that represents a percentage:
# 95 = 95%.
#
MAX_USAGE="90"
#
# Failsafe mechansim. Delete a maxium of MAX_CYCLES files, raise an error after
# that. Prevents possible runaway script. Disable by choosing a high value.
#
MAX_CYCLES=10


show_header () {

echo
echo DELETE OLD FILES $VERSION
echo

}

show_header

reset () {
CYCLES=0
OLDEST_FILE=""
OLDEST_DATE=0
ARCH=`uname`
}

reset

if [ -z "$MOUNT" ] || [ ! -e "$MOUNT" ] || [ ! -d "$MOUNT" ] || [ -z "$MAX_USAGE" ]
then
echo "Usage: $0 <mountpoint> <threshold>"
echo "Where threshold is a percentage."
echo
echo "Example: $0 /storage 90"
echo "If disk usage of /storage exceeds 90% the oldest"
echo "file(s) will be deleted until usage is below 90%."
echo 
echo "Wrong command line arguments or another error:"
echo 
echo "- Directory not provided as argument or"
echo "- Directory does not exist or"
echo "- Argument is not a directory or"
echo "- no/wrong percentage supplied as argument."
echo
exit 1
fi

check_capacity () {

USAGE=`df -h "$MOUNT"| tail -1 | awk '{ print $5 }' | sed s/%//g`
if [ ! "$?" == "0" ]    
then
echo "Error: mountpoint $MOUNT not found in df output."
exit 1
fi

if [ -z "$USAGE" ]
then
echo "Didn't get usage information of $MOUNT"
echo "Mountpoint does not exist or please remove trailing slash."
exit 1
fi

if [ "$USAGE" -gt "$MAX_USAGE" ]
then
echo "Usage of $USAGE% exceeded limit of $MAX_USAGE percent."
return 0
else
echo "Usage of $USAGE% is within limit of $MAX_USAGE percent."
return 1
fi
}

check_age () {

FILE="$1"
if [ "$ARCH" == "Linux" ]
then
FILE_DATE=`stat -c %Z "$FILE"`
elif [ "$ARCH" == "Darwin" ]
then
FILE_DATE=`stat -f %Sm -t %s "$FILE"`
else
echo "Error: unsupported architecture."
echo "Send a patch for the correct stat arguments for your architecture."
fi

NOW=`date +%s`
AGE=$((NOW-FILE_DATE))
if [ "$AGE" -gt "$OLDEST_DATE" ]
then
export OLDEST_DATE="$AGE"
export OLDEST_FILE="$FILE"
fi
}

process_file () {

FILE="$1"

#
# Replace the following commands with wathever you want to do with 
# this file. You can delete files but also move files or do something else.
#
echo "Deleting oldest file $FILE"
rm -f "$FILE"
}

while check_capacity
do
if [ "$CYCLES" -gt "$MAX_CYCLES" ]
then
echo "Error: after $MAX_CYCLES deleted files still not enough free space."
exit 1
fi


FILES=`find "$MOUNT" -type f`

IFS=$'\n'
for x in $FILES
do
check_age "$x"
done

if [ -e "$OLDEST_FILE" ]
then
#
# Do something with file.
#
process_file "$OLDEST_FILE"
else
echo "Error: somehow, item $OLDEST_FILE disappeared."
fi
((CYCLES++))
done
echo

我在 sudo crontab -e 中輸入:

#Borrowed from anacron SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#End borrowed from anacron
* * * * * /home/pi/deleteoldfiles.sh

抱歉,帖子很長,我試圖解釋發生了什麼,也許我會得到一些幫助。謝謝 !

答案1

我做到了 !我刪除了 sudo crontab -e 中的所有內容並僅在 crontab -e 中輸入了這一行

* * * * * sudo bash -x /home/pi/deleteoldfiles.sh > /home/pi/output.txt 2>&1

它正在工作,我有一個日誌文件,我可以追蹤它是否工作正常。謝謝你們 !

答案2

很好的腳本,但你應該在後面添加這兩行((CYCLES++))

OLDEST_FILE=""
OLDEST_DATE=0

刪除新的最舊的檔案。

相關內容