Script needed to wait for existence of multiple files

Hi Forum.

I need a script to wait for all multiple trigger files to be present or else go to sleep for 10 seconds (Number of trigger files can vary). I tried to search on the forum but was not able to find a solution.

I found this code on this forum but it's not working as expected:


for i in `cat ${TMP_TRG_FILE}`  -- where TMP_TRG_FILE contains trigger files trg1.txt trg2.txt trg3.txt
do

   if [ -e "$i" ]
   then
       echo "File Exists"
       break
   else
       sleep 10
   fi
 
done
  • for loop stops after a number of iterations instead of going to sleep and waiting for the missing trigger files.

Any help is greatly appreciated.

Thanks

When one of the files contained in TMP_TRG_FILE does exist, the loop terminates. this is what the code is supposed to do.
If you do not want the loop to quit, remove "break", then the loop will run forever.

Hi Kevintse.

I removed the "break" statement as you suggested but the loop terminated after going to sleep 2 times and program terminated.

file TMP_TRG_FILE contains following entries:

/tmp/wf_fact1_a_sessions_completed.trg
/tmp/wf_fact1_b_sessions_completed.trg
/tmp/wf_fact1_d_sessions_completed.trg
/tmp/wf_fact1_e_sessions_completed.trg
/tmp/wf_fact2_b_sessions_completed.trg
/tmp/wf_fact2_c_sessions_completed.trg

There exists only 4 trigger files and 2 missing in /tmp:
wf_fact1_a_sessions_completed.trg
wf_fact1_b_sessions_completed.trg
wf_fact1_d_sessions_completed.trg
wf_fact1_e_sessions_completed.trg

Here's the output results:
file: /tmp/wf_fact1_a_sessions_completed.trg
File Exists
file: /tmp/wf_fact1_b_sessions_completed.trg
File Exists
file: /tmp/wf_fact1_d_sessions_completed.trg
File Exists
file: /tmp/wf_fact1_e_sessions_completed.trg
File Exists
file: /tmp/wf_fact2_b_sessions_completed.trg
sleeping...
file: /tmp/wf_fact2_c_sessions_completed.trg
sleeping...
$

Your concept of re-using other people's code is great, of course, but I think that the logic of the code you have is to make one pass through the reference file TMP_TRG_FILE and then finish, as a for loop does.

Perhaps the following would be a more suitable variation:-

#!/bin/ksh
Err_Flag=1
until [ $Err_Flag -eq 0 ]
do
   Err_Flag=0
   for i in `cat ${TMP_TRG_FILE}`      # where TMP_TRG_FILE contains trigger files trg1.txt trg2.txt trg3.txt
   do
      if [ -e "$i" ]
      then
         >/dev/null                    # File Exists, do nothing
      else
         echo "Still waiting for $i"
         ((Err_Flag=$Err_Flag+1))      # Increment counter to show we're still waiting
      fi
   done
   echo "Waiting for $Err_Flag files."
   sleep 10
done

This way, your loop continues until all the files exist.

I must admit that I haven't tested this, is dry written code into this page, but you should get the basic plan of it.

I hope that this helps. Do let us know how you get on.

Robin
Liberpool/Blackburn
UK

try this:

while read trigger_file
do
   while [[ ! -e $trigger_file ]]
   do
      print "Waiting for trigger file: $trigger_file"
      sleep 10
   done
done < $TMP_TRG_FILE

all the trigger files would exist when the outer while loops ends.

Thank you guys for your quick responses. I will give a try to your suggested solutions.

while [[ $(ls /tmp/wf_fact@(1_[abde]|2_[bc])_sessions_completed.trg | wc -l) -lt `cat ./TMP_TRG_FILE | wc -l` ]]
do
     sleep 10
done

In my opinion, it is a quite bad idea to generate a display every 10 seconds but ... here you go

while :
do
     MISSING=$(ls /tmp/wf_fact@(1_[abde]|2_[bc])_sessions_completed.trg | grep -vf - TMP_TRG_FILE)
     [[ -z $MISSING ]] && break
     echo "Following Trigger still missing : $MISSING"
     sleep 10
done

Tested on linux, works fine (using ksh)