Link to home
Start Free TrialLog in
Avatar of xbox360dp
xbox360dp

asked on

Help with a Script that will rename a file when it's not longer being written to

Gurus,

I need a script that will rename a file when it's not longer being written to.

Step 1: Check and make sure the file isn't being written to.
Step 2: Once file isn't being written to , rename all files in /opt/LAS/tranfer/export/.* to .complete

Any help would be greatly appreciated.
Avatar of arnold
arnold
Flag of United States of America image

You can use stat($filename)[9] I think that will give you the last modified UNIX timestamp.
How long do you want to wait I.e. After a minutes, no more update So?

http://perldoc.perl.org/functions/stat.html

Checking on last modified..

If you need to extract data while it is being written, you can use a white loop with seek, stat, position writeout .......
Avatar of xbox360dp
xbox360dp

ASKER

Hi Arnold,

I would like the file to be renamed when there is no more data written. Could you give me an example?
Cron job
#!/usr/bin/perl

my $dir='/opt/LAS/transfer/export';

open FILE , "ls $dir |" || die "unable to list contents of $dir:$!\n";

while <FILE> {
chmop();
print "processing $dir/$_\n";
if ( localtime() - stat($dir/$_)[9] >= 90 ) {
          #file has it been modified in 90 seconds
           rename "$dir/$_", "$dir/$_.complete";
}

}
close(FILE);

Open in new window


Note it does not check whether the file to which it is being renamed does not exist.

Test first.
Thanks Arnold!

I tried to test this code in a shell script and got the following errors:

syntax error at ./mod_export.sh line 7, near "while <FILE>"
syntax error at ./mod_export.sh line 10, near ")["
Execution of ./mod_export.sh aborted due to compilation errors.
ASKER CERTIFIED SOLUTION
Avatar of FishMonger
FishMonger
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial