Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep polling file in a directory till it arrives in Unix

Tags:

unix

I want to keep polling file till it arrives at the location for 1 hour.

My dir : /home/stage

File Name (which I am looking for): abc.txt

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.

Is there any way to achieve this in Unix?

like image 289
Pooja25 Avatar asked Nov 29 '22 12:11

Pooja25


1 Answers

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
like image 141
twalberg Avatar answered Dec 26 '22 15:12

twalberg