watching for a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting watching for a file
# 1  
Old 07-29-2010
watching for a file

Hi dears,


The script should watch for a file in particular directory. If the file not come into the directory even after 30 seconds from execution of the script, then script should exit saying “file not arrived”


Any help ??
# 2  
Old 07-29-2010
Where did you get stuck?
# 3  
Old 07-29-2010
thank you ..This how i tried so far ..
Code:
 
while [ 1 -gt 0 ]
do
  find <dirname> -type f -name <fileName>
   if [ $? -eq 0 ]
   then
     echo file arrived
     else
     echo file yet to arrive. wait
     fi
done

But where and how to mention wait for the file max 30 seconds...

Last edited by zaxxon; 07-29-2010 at 11:23 AM.. Reason: code tag correction
# 4  
Old 07-29-2010
When you use code tags (which is good Smilie ) then please use square brackets instead of < and > Smilie

Ok to your problem - here is a suggestion how it could look like:
Code:
#!/usr/bin/ksh

MYFILE=/here/it/is/myfile

Z=1
while (( $Z < 30 )); do
   if [ ! -e $MYFILE ]; then
      let Z=$Z+1
      sleep 1
   else
      echo it arrived
      exit 0
   fi
done

echo 30 seconds are over and it did not arrive
exit 1


Last edited by zaxxon; 07-29-2010 at 11:31 AM.. Reason: changed failure message
# 5  
Old 07-29-2010
Lateral though way of letting the script flow after the file arrives.

Beware that the presence of a filename in a directory does not mean that the process writing the file has finished.
The process writing the file could write the file under a temporary name and then rename the file to its correct name at the end of the transfer.


Code:
filename="myfile"
for T in 5 5 5 5 5 5 X
do
        if [ "${T}" = "X" ]
        then
                echo "File ${filename} did not arrive"
                exit
        fi
        if [ -f "${filename}" ]
        then
                echo "File ${filename} arrived"
                break
        fi
        echo "File ${filename} yet to arrive. Waiting ${T} seconds"
        sleep ${T}
done
#
# Processing of ${filename} goes here

Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

2. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

3. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

4. UNIX for Dummies Questions & Answers

sed remove expression from output I'm watching

I'm watching a particular expression as it is appended in a line to a file: tail -f LOG | sed -n /"$@"/p So whatever value I pass into this script will tail -f the file, but only show me lines that contain the value: lgwatch expression However some of the output contains a #20 control... (8 Replies)
Discussion started by: MaindotC
8 Replies

5. Shell Programming and Scripting

Actively watching for running processes

Here's a fun one... What's the best way to script looking for a process when it runs? For example, what if a process is running that's been set up graphically, which you would like the entire command line string for? Say it's rpm for the sake of having a command to use as an example. You want... (2 Replies)
Discussion started by: sysera
2 Replies
Login or Register to Ask a Question