我的意图是每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/$1 ]; then
echo "File \"/home/test/$1" exists”
else
echo "File not found"
fi
更新代码:
MESSAGE="Some message"
SUBJECT="Some subject"
RECIPIENT="somebody@somewhere.com"
filename=$1;
# 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条目
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
但如果我找到该文件,我真的不想检查发生,我很困惑该怎么做。
如果我在指定时间内没有找到该文件,我将不得不发送邮件。 还有其他方法可以解决这个问题。
答案 0 :(得分:1)
这样的事情在cron
:
#!/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/"$1" ]; then
echo "File /home/test/$1 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
答案 1 :(得分:1)
其他解决方案的变体,允许您在命令行指定持续时间和间隔,例如,间隔5分钟运行30分钟,每隔3分钟运行60分钟等等。
注意:借用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
答案 2 :(得分:0)
只需从cron运行一次脚本,让脚本休眠5分钟,直到经过所需的时间,然后发送电子邮件。这样,所有状态都包含在脚本的单个运行中,并且您不需要将任何内容写入磁盘以记住先前运行的状态。
答案 3 :(得分:0)
如果找到文件,您可以创建时间戳文件:
if [ -f /home/test/$1 ]; then
echo "File \"/home/test/$1" exists”
touch /tmp/$1.timestamp
else
echo "File not found"
fi
如果时间是X:30(可能是一分钟的容差),并且今天没有时间戳文件,您可以发送邮件。
触摸将使用当前时间更新现有文件,如果不存在文件,则创建一个新文件。