Look for a file,if not found wait and look again


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Look for a file,if not found wait and look again
# 1  
Old 07-31-2013
Look for a file,if not found wait and look again

Hi All, i have a requirement
I created a file list as below :

Code:
more abc_file_list.txt

Rem_DD.csv
Rem_Non.csv
Rem_Ld.csv
CC_Ld_Non_IRA.csv ...

Above are the 4 files (For now ,we dont know how many in the file) which need to be present else need to get an email for missing files.

script needs to look for all files.. if any file doesnot present on the source directory wait for 60 sec and again look for the missing file if not found look for the next file.. i written the below script but i was unable look for second time
Code:
#!/usr/bin/ksh
set -vx
###### Variables #########
FILE_LIST=/home/username/abc_file_list.txt
DATA_SRC_DIR=/home/username
Log_file=/home/username/test.log
SUPPORT_EMAIL=abc@someemail.com
NumofFiles=`wc -l<"$FILE_LIST"`
#Check for the CISS files in sleep mode
dt=`date +"%Y-%m-%d %X"`
printf "started at $dt \n " > $Log_file
FILE_FOUND=N
export i=0
while [ $FILE_FOUND = N ]
do
  ip=$FILE_LIST
  for f in `cat "$ip"`
  do
    if [ -r $f ]; then
      printf " CISS File $f exists in "$DATA_SRC_DIR" \n" >>$Log_file
      FILE_FOUND=Y
      export i=`expr $i + 1`
    else
      export i=`expr $i + 1`
      printf " abc File $f Not received in "$DATA_SRC_DIR" \n" >>$Log_file
      #Check for No.of tries
      if [ $i -eq $NumofFiles ]; then
        cat $Log_file| mailx -s "abc File $f Not received and Reached MAX checks" $SUPPORT_EMAIL
        exit 1
      else
        sleep 60
      fi
    fi
  done < "$FILE_LIST"
done


Last edited by Scott; 07-31-2013 at 01:06 PM.. Reason: Removed formatting; added code tags
# 2  
Old 07-31-2013
This will show you immediately which of the files in abc_file_list.txt is missing in the PWD:
Code:
grep -hv "$(ls $(< abc_file_list.txt) 2>-)" abc_file_list.txt

Put a
Code:
 while ((i++ < $MAX)); do ... done

around it to check MAX times, and then send your alert mail. Should work in recent shells (bash, ksh, ...)
# 3  
Old 07-31-2013
Not sure if this is what you are looking for but this is something I've used before in the past. This will loop through every 60 secounds looking for any file that is not found and sending out an alert of the files. Once all files are there the script will stop. Not sure if you need to take action if all files are found.

Code:
#!/usr/bin/ksh
set -vx

FILE_LIST=abc_file_list.txt
DATA_SRC_DIR=/home/username
SUPPORT_EMAIL=abc@someemail.com

NumofFiles=`wc -l ${FILE_LIST}|awk '{print $1}'`

filecheck ()
{
  x=0
  for file in `cat ${FILE_LIST}`
  do
      if [ -f ${file} ];then
          x=$(( ${x} + 1 ))
      else
          missing="${missing} ${file}\n"
      fi
  done
  missing=`echo -e "${missing}"|awk '{if ( $1 != "") print $0}'|sed "/^$/d"|sed "s/^ *//g"`

}


filecheck

if (( ${x} != ${NumofFiles} ));then
     until [ ${x} = ${NumofFiles} ]
     do
        if [ "${missing}" ];then
             MSG=`(echo "Missing Files:";echo;echo "${missing}")`
             echo "${MSG}"|mailx -s "abc File Not received in "$DATA_SRC_DIR" " ${SUPPORT_EMAIL}
             missing=""
        fi
        sleep 60
        filecheck
     done
fi

# 4  
Old 08-02-2013
bash, ksh:
Code:
cd somedatasrcdir
alldone=0
cnt=0
MAX=1000
while ((  alldone==0  ))
do

        ok=1
        ((cnt+=1))
        msg=""
        while read file
                if [ ! -f "$file" ] ; then
                        ok=0
                        msg="$msg $file"
                fi
        do < filelist.txt

        ((  ok==1     )) && alldone=1 && continue
        ((  cnt>MAX )) && alldone=1 && continue

        sleep 60
done


if ((  ok==0  )) ; then
        echo "file(s): $msg not received" | mailx ...
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Wait for file to get copied in server path and then proceed

Hi, I have a requirement to create below script: Script must run infinitely in background. It will check a particular type of file to be copied in specific folder of server Script must wait till any file gets fully copied ..(important) and then It will read that file Experts please... (4 Replies)
Discussion started by: Vikash163
4 Replies

2. Shell Programming and Scripting

calling a shell script in background and wait using "wait" in while loop

Hi, I am facing a strange issue, when i call a script from my while loop in background it doesnt go in background, despite the wait i put below the whil loop it goes forward even before the process put in background is completed. cat abc.txt | while read -u4 line do #if line contains #... (2 Replies)
Discussion started by: mihirvora16
2 Replies

3. Shell Programming and Scripting

Script to wait until file is updated

Hello, I need to evaluate (under BASH) if the certain file has been updated or not. If the file still wasn't updated, script should wait. The script picks up the time stamp of the file using command OldTimestamp=$(date -r $MyDir/$MyFile), but I don't know how to code a waiting loop with new and... (6 Replies)
Discussion started by: sameucho
6 Replies

4. Shell Programming and Scripting

Move files one at the time and wait until the previous file is handled

I'm a novice at unix and need it more and more to do my work. I seem running into problems getting this script "attempt" to work: I need to copy all files in a directory, which is containing 22000 files, into a directory one level up. There a tool monitors the content of the dir and processes... (2 Replies)
Discussion started by: compasscard
2 Replies

5. Shell Programming and Scripting

event or file wait scenario

i want to do 2 things 1) i should wait for a file called A.txt to fall on a specific path on the server 2) once the file arrives immediately another script called B.sh should be executed. Could you please throw somelight on this. Any code if already present that would be helpful. ... (13 Replies)
Discussion started by: rajesh_tns
13 Replies

6. Shell Programming and Scripting

Cron wait on dynaminc trigger file

I have to schedule a script in cron that watch for a trigger file named as xyz_extract_file_20081201.null (the YYYYMMDD portion would change every day). The wait/wakeup time would be 1 min and the date portion will always be current date (Eg., the trigger file named as... (2 Replies)
Discussion started by: shekharaj
2 Replies

7. Shell Programming and Scripting

wait command - cat it wait for not-chile process?

Did not use 'wait' yet. How I understand by now the wait works only for child processes, started background. Is there any other way to watch completion of any, not related process (at least, a process, owned by the same user?) I need to start a background process, witch will be waiting... (2 Replies)
Discussion started by: alex_5161
2 Replies

8. Shell Programming and Scripting

Need to execute 2 scripts, wait, execute 2 more wait, till end of file

:cool: I need to execute a shell script to do the following: cat a file run two back ground processes using the first two values from the file wait till those background processes finish run two more background processes using the next two values from the file wait till those background... (1 Reply)
Discussion started by: halo98
1 Replies

9. UNIX for Dummies Questions & Answers

How to find the File Age and wait for that...

Hi, I want to know my file is 1 hr 30 min old or not, If 1 hr 30 min old I will do some tasks in that file.. other wise I will wait to 1 hr 30 min and then do the tasks.. how to do it in Unix script? any idea? (3 Replies)
Discussion started by: redlotus72
3 Replies

10. Shell Programming and Scripting

Lock a file. AND Wait if file is locked

Hi, I want to do the foll steps: 1. Check if someone has a lock on my file1. 2. if file1 is locked by any other user wait in a loop till another user releases lock 3. when lock released, lock file1. 4. do procesing (write) on file1. 5. processing complete. release lock on file1. ... (2 Replies)
Discussion started by: sunil_neha
2 Replies
Login or Register to Ask a Question