Script to wait until file is updated


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to wait until file is updated
# 1  
Old 05-26-2010
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
Code:
OldTimestamp=$(date -r $MyDir/$MyFile)

, but I don't know how to code a waiting loop with new and old time stamps comparision (leaving condition is the timestamps are different).

Would anybody help?
Thanks.

Last edited by sameucho; 05-26-2010 at 11:00 AM..
# 2  
Old 05-26-2010
What about checking if the file is updated using file size? If you are ok, I do have a script ready.
-Nithin.
# 3  
Old 05-26-2010
I try something like this:

Code:
NewTimestamp=$OldTimestamp; 
while [$NewTimestamp -eq $OldTimestamp ];  do 
   sleep1; 
   NewTimestamp=$(date -r $CsvDir/$CsvFile); 
   echo old: $OldTimestamp; echo new: $NewTimestamp; 
done;

But it doesn't work. I get error:

-bash: [Mon: command not found

---------- Post updated at 04:16 PM ---------- Previous update was at 04:12 PM ----------

Quote:
Originally Posted by bsnithin
What about checking if the file is updated using file size? If you are ok, I do have a script ready.
-Nithin.
I think this approach also could help. Can you post your code?

Anyhow the solution with timestamps is still apreciated (at least for know-how purpose if for nothing else).
# 4  
Old 05-26-2010
Quote:
Originally Posted by sameucho
I try something like this:

Code:
NewTimestamp=$OldTimestamp; 
while [$NewTimestamp -eq $OldTimestamp ];  do 
   sleep1; 
   NewTimestamp=$(date -r $CsvDir/$CsvFile); 
   echo old: $OldTimestamp; echo new: $NewTimestamp; 
done;

But it doesn't work. I get error:

-bash: [Mon: command not found[COLOR="#738fbf"]

1. you must have single space on both side of opening and closing square bracket.
2. sleep1 should be sleep 1
3. In comparison, you didn't use quotes. ( you will have spaces in both the variables)
4. you are comparing string with integer operator.

Code:
#!/usr/bin/ksh
NewTimestamp=$OldTimestamp
while [ "$NewTimestamp" = "$OldTimestamp" ];  do 
   sleep 1 
   NewTimestamp="$(date -r $CsvDir/$CsvFile)"
   echo "old: $OldTimestamp"; echo "new: $NewTimestamp"
done

EDIT: semicolon is necessary only when you have two separate commands on a single line.
# 5  
Old 05-26-2010
Quote:
Originally Posted by anchal_khare
1. you must have single space on both side of opening and closing square bracket.
2. sleep1 should be sleep 1
3. In comparison, you didn't use quotes. ( you will have spaces in both the variables)
4. you are comparing string with integer operator.

Code:
#!/usr/bin/ksh
NewTimestamp=$OldTimestamp
while [ "$NewTimestamp" = "$OldTimestamp" ];  do 
   sleep 1 
   NewTimestamp="$(date -r $CsvDir/$CsvFile)"
   echo "old: $OldTimestamp"; echo "new: $NewTimestamp"
done

EDIT: semicolon is necessary only when you have two separate commands on a single line.

Yes, this work! Thanks a lot.

------

Bsnithin, what's about your solution about filesize comparision? I am still interesting to know it.

---------- Post updated at 05:46 PM ---------- Previous update was at 04:46 PM ----------

Hi, can you still help me with this please? ... I am fighting with the syntaxt. The same script as before, only an additional (logical AND) condition was added - wait loop should be terminated either if timestamps differs or if Countdown counter PkillTimeout is 0:

Code:
MaxWaitPkillResponseTimeout=30;  
PkillTimeout=$MaxWaitPkillResponseTimeout; 
MyDir=/DIR;
MyFile=$(ls  $MyDir | tail -1); 
OldTimestamp=$(date -r $MyDir/$MyFile); 
NewTimestamp=$OldTimestamp;  
while [ (( "$NewTimestamp" = "$OldTimestamp"  ) && (PkillTimeout)) ]; do 
  sleep 1; 
   NewTimestamp=$(date -r $CsvDir/$CsvFile); 
  let  PkillTimeout-=1;
done;  
echo $NewTimestamp

# 6  
Old 05-26-2010
Here is it
Code:
#!/bin/ksh

if [ -z $1 ]
then
 echo "Enter input file" ;
 exit 1 ;
elif [ ! -r $1 ]
then
 echo "The file $1 is not readable" ;
 exit 2 ;
else
 file=$1
 while [ 1 -gt 0 ]
 do
 Oldsize=`ls -ltr ${file} | cut -d" " -f17` ;
 sleep 1 ;
 Newsize=`ls -ltr ${file} | cut -d" " -f17` ;
 if [ ${Oldsize} -ne ${Newsize} ]
 then
  echo "File size changed" ;
 else
  echo "No change. Wait" ;
 fi
 done
fi

-Nithin.
# 7  
Old 05-26-2010
Quote:
Code:
  while [ 1 -gt 0 ]

Thanks. But if I understand it well, this script is not self-terminating. It only gives you the notification. Isn't it?

---------- Post updated at 06:13 PM ---------- Previous update was at 05:59 PM ----------

Yeh, this seem to be correct:

Code:
while [  "$NewTimestamp" = "$OldTimestamp" ] && [ $PkillTimeout -gt 0 ]; do 
  OldTimestamp=$(date -r $CsvDir/$CsvFile); 
  let  PkillTimeout-=1; 
done;

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script using Wait

Hi, written a script which uses wait as follows Main.sh #!/usr/bin/ksh nohup scrpit1 1 & pid_1=$! nohup scrpit1 2 & pid_2=$! wait $pid_1 wait $pid_2 nohup scrpit1 3 & pid_1=$! nohup scrpit1 4 & (1 Reply)
Discussion started by: krux_rap
1 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 send email if new file is added when folder is updated

Hi, I have a script which updates a repository which is run manually. When this happens either no or new files will be added to a specific folder. If there are new files that have been added, an email needs to be sent. How can i check a folder for any new files after the update repos script has... (0 Replies)
Discussion started by: acc01
0 Replies

4. Shell Programming and Scripting

Wait function in a script

Hi everyone, I need some help to create a script. This script have to create a file once all the process inside are finish. Here how I want to do : #!/bin/ksh /home/oracle/save1.ksh & proc_id1=$! /home/oracle/save2.ksh & proc_id2=$! /home/oracle/save3.ksh & proc_id3=$! ... (4 Replies)
Discussion started by: remfleyf
4 Replies

5. AIX

Aix script to monitor when a file has been updated

Hi Im looking to create a Aix script to monitor a file for once it's been updated and then send an email. I dont have great scripting knowledge and have only come up with the following so far while true do find /home/test/AMQ* -mmin 1 > /home/test/scripts/output/MQERRORS exec... (2 Replies)
Discussion started by: elmesy
2 Replies

6. Shell Programming and Scripting

To force a script to wait for another.

Hi All! Here is the problem, I'm trying to develop a script that can help me with the raid creation, but, till now, I have been dealing for more than a week and I still didn't achieve any satisfactory results. :confused: Here is the code to execute: # mdadm --manage /dev/md0 --add... (4 Replies)
Discussion started by: Ne7o7
4 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. Shell Programming and Scripting

make my script wait

is there a way to make my script wait before doing something without using the "sleep _" command? (4 Replies)
Discussion started by: Blip
4 Replies
Login or Register to Ask a Question