首页 > 解决方案 > 如果 cron 循环中的多个条件中的条件逻辑错误

问题描述

我需要在 cron 中运行 2 个循环,每 5 分钟运行一次

*/5 * * *  *

按 cron 循环 1. 每 5 分钟工作一次,检查文件是否已上传。($yesterday 意味着具有回溯名称的文件)

第一个 cron-loop 对我来说很好,第二个 cron-loop 我无法解决,第二个循环有 3 个条件

1. 找到 $yesterday.zip 后应该就可以了

2. 它应该只在 $yesterday.zip 之后工作一次(因为它的 cron 所以它在 $yesterday.zip 找到后每 5 分钟工作一次)

3. 在 $yesterday.zip 下载之前 00:00 不应该工作

($yesterday 文件没有固定时间下载,所以我每 5 分钟运行一次 cron)我做了这个(写在下面,所以你们。伙计们不要认为我没有努力,也没有说显示示例代码,只需要一个带有 cron 的 if 语句包括这些3个条件)

FILE=/fullpath/$yesterday.zip
if test -f "$FILE"; then
touch /fullpath/loop2.txt ##########for loop 2
echo "I am the best"
else
cd /fullpath/
wget -r -np -nH "url/$yesterday.zip" ###########it should be 20+ mb file
find . -name "*.zip" -type 'f' -size -160k -delete ########## it will delete is some garbage downloaded
rm -rf /fullpath/loop2.txt  ########## delete the file so it stopped loop 2 for working evry 5 minutes .
fi

FILE2=/fullpath/loop2.txt
if test -f "$FILE2"; then
echo -e "Script will work only once" | mailx -v -s "Script will work only once"  myemail@gmail.com
else
echo "full script work"
touch /fullpath/loop2.txt
fi

你们可以忽略我上面的代码,简单地让我知道这样 3 个条件循环的 if 语句

标签: bashloopsif-statementcronlogic

解决方案


我会使用这样的东西:

if lockfile -r0 /tmp/lockfile_$(date +%F); then #only run if there's no lockfile for the day
    cd /fullpath/
    # while we don't have a correct file (absent or truncated)
    while [[ ! -f "$yesterday.zip" ]] || [[ $(stat -c %s "$yesterday.zip") < 20971520 ]]; do
      wget -r -np -nH "url/$yesterday.zip"  # try to download it
      if [ $? -ne 0 ]; then # if the file isn't available yet
        rm /tmp/lockfile_$(date +%F) # delete the lock in order to attempt the download again in 5"
      fi
    done
fi

推荐阅读