如何将轮询文件保留在目录中,直到它到达 Unix [英] How to keep polling file in a directory till it arrives in Unix
问题描述
我想保留轮询文件,直到它到达该位置 1 小时.
I want to keep polling file till it arrives at the location for 1 hour.
我的目录:/home/stage
文件名(我正在寻找):abc.txt
我想将轮询目录 /home/stage
保持 1 小时,但在 1 小时内,如果 abc.txt
文件到达,则它应该停止轮询并显示消息文件到达
否则1小时后应该显示文件没有到达
.
I want to keep polling directory /home/stage
for 1 hour but within the 1 hour if abc.txt
file arrives then it should stop polling and should display the message file arrived
otherwise after 1 hour it should display that file has not arrived
.
有没有办法在 Unix 中实现这一点?
Is there any way to achieve this in Unix?
推荐答案
另一种 bash
方法,不依赖陷阱处理程序和信号,以防您的更大范围已经将它们用于其他用途:>
Another bash
method, not relying on trap handlers and signals, in case your larger scope already uses them for other things:
#!/bin/bash
interval=60
((end_time=${SECONDS}+3600))
directory=${HOME}
file=abc.txt
while ((${SECONDS} < ${end_time}))
do
if [[ -r ${directory}/${file} ]]
then
echo "File has arrived."
exit 0
fi
sleep ${interval}
done
echo "File did not arrive."
exit 1
这篇关于如何将轮询文件保留在目录中,直到它到达 Unix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!