使用 shell 脚本检查从上午 9 点到 9:30 的文件是否存在,并停止检查是否在其间的任何时间找到文件

Check the existence of file from 9 to 9:30 am using shell script and stop checking if file is found any time in between

我的意图是每 5 分钟在不同的时间检查文件,以下是我的目标和实现的内容。

目标:

  To check for file file1 from  9 to  9:30
  To check for file file2 from 11 to 11:30
  To check for file file3 from 10 to 10:30
  To check for file file4 from  4 to  4:30

Check_File.sh

if [ -f /home/test/ ];  then
  echo "File \"/home/test/" exists”
else 
  echo "File not found"
fi

updated code:

MESSAGE="Some message"
SUBJECT="Some subject"
RECIPIENT="somebody@somewhere.com"
filename=;
# Test for 30 minutes
for ((i=0;i<7;i++)) ; do
  echo "searching for file ${filename} `date` "
  if [ -f ./${filename} ];  then
   echo "File /home/test/${filename} exists"
   exit 0
  fi
 # Sleep 5 minutes
  echo "sleeping `date` as ${filename} file not found"
  sleep 300
done
echo "MAIL:File not found"

cronjob entry

00-30/5  9 * * * /checkfile.sh file1 > /path/to/log/file/job1.log 2>&1
00-30/5 11 * * * /checkfile.sh file2 > /path/to/log/file/job2.log 2>&1
00-30/5 10 * * * /checkfile.sh file3 > /path/to/log/file/job3.log 2>&1
00-30/5  4 * * * /checkfile.sh file4 > /path/to/log/file/job4.log 2>&1

但是如果我找到了这个文件,我真的不想检查它,我不知道该怎么做。

如果我在指定的时间内没有找到文件,我将不得不发送邮件。 有没有其他方法可以解决这个问题。

只需运行 cron 中的脚本一次,让脚本休眠 5 分钟,直到所需的时间过去,然后发送电子邮件。这样,所有状态都包含在脚本的单个 运行 中,您无需向磁盘写入任何内容即可记住先前 运行 的状态。

如果找到文件,您可以创建一个时间戳文件:

if [ -f /home/test/ ];  then
  echo "File \"/home/test/" exists”
  touch /tmp/.timestamp
else 
  echo "File not found"
fi

如果时间是X:30(可能容差一分钟),而且没有今天的时间戳文件,可以发邮件。

Touch 将使用当前时间更新现有文件并创建一个新文件(如果不存在文件)。

类似这样的事情 运行 在 cron 的 30 分钟开始时只有一次:

#!/bin/bash

MESSAGE="Some message"
SUBJECT="Some subject"
RECIPIENT="somebody@somewhere.com"

# Test for 30 minutes
for ((i=0;i<7;i++)) ; do
   if [ -f /home/test/"" ];  then
      echo "File /home/test/ exists”
      exit 0
   fi
   # Sleep 5 minutes
   sleep 300
done

# Failed to find file in specified time
echo "$MESSAGE" | mailx -s "$SUBJECT" $RECIPIENT

一种稍微不同的做法是这样的:

#!/bin/bash

MESSAGE="Some message"
SUBJECT="Some subject"
RECIPIENT="somebody@somewhere.com"

start=$SECONDS
# Test for 30 minutes
while : ; do
   if [ -f "/home/test/" ];  then
      exit 0
   fi
   ((elapsed=SECONDS-start))
   # Break out of loop if 30 minutes have elapsed
   [ $elapsed -ge 1800 ] && break
   # Sleep 5 minutes
   sleep 300
done
# Failed to find file in specified time
echo "$MESSAGE" | mailx -s "$SUBJECT" $RECIPIENT

其他解决方案的变体,允许您在命令行指定持续时间和间隔,例如,运行 30 分钟 @ 5 分钟间隔,运行 60 分钟 @ 3最小间隔等

注意:借用 Mark Setchell 的 mailx 代码 ...

#!/bin/bash

filename=${1:-undefined}
durationmins=${2:-30}      # default to 30 min duration
sleepmins=${3:-5}          # default to  5 min intervals

MESSAGE="Some message"
SUBJECT="Some subject"
RECIPIENT="somebody@somewhere.com"

while true
do
        dt=$(date '+%Y/%m/%d %H:%M:%S')

        [ -f "${filename}" ]                        && \
        echo "${dt} - File '${filename}' exists"    && \
        exit 0

        durationmins=$((durationmins - sleepmins))

        [ ${durationmins} -lt 0 ] && break

        echo "${dt} - File '${filename}' not found, sleeping ${sleepmins} minute(s) ..."

        sleep $((sleepmins * 60))
done

dt=$(date '+%Y/%m/%d %H:%M:%S')

echo "${dt} - File '${filename}' not found, sending email ..."

echo "$MESSAGE" | mailx -s "$SUBJECT" $RECIPIENT

一些示例调用:

$ ./checkfile.sh file1 30  5        # check for 30 mins @  5 min intervals; 7 checks performed
$ ./checkfile.sh file2 60 10        # check for 60 mins @ 10 min intervals; 7 checks performed
$ ./checkfile.sh file3 15  7        # check for 15 mins @  7 min intervals; 3 checks performed
$ ./checkfile.sh file4              # defaults: check for 30 mins @ 5 min intervals; 7 checks performed