A variation of Wiggins' approach is to pre or append the PID to the file
name.  This can potentially allow multiple processes of the same type to run
simultaneously without stomping on each other while they are running and can
aid in tracking down those production problems - assuming you are logging
your PID, or just figuring out which zombie process owns which file...

it's a little more work to ignore, or you can use both ideas at the same
time and ignore files starting with dot.

-Tom Kinzer

-----Original Message-----
From: Wiggins d Anconia [mailto:[EMAIL PROTECTED]
Sent: Friday, December 12, 2003 6:37 AM
To: [EMAIL PROTECTED]
Subject: Re: Move a file




> Neill Taylor wrote:
> >
> > I am writing a script which will poll a directory and then move  any
files
> > in the directory to a new directory dependant upon its name.
> >
> > The problem I think I might have is that the script may try to pick
a file
> > that is still being copied into the directory whilst the application is
> > still trying to write the file.
> >
> > What is the best method in Perl to prevent errors.
>
> Hi Neill.
>
> If your application is well-behaved then it may lock the file
> for writing. If your Perl program asks for an exclusive lock
> then it will be granted if it no longer being accessed.
> Take a look at
>
>   perldoc -f flock
>
> Another way is to check the size of file. A file will often
> show a size of zero until it is closed by the writing
> application.
>
> Finally, if only one file at a time is being written then
> just sort the them in order of creation date and move all
> but the latest one.
>
> What works you will find only by experimentation.
>

Depending on what is writing the files, aka another controllable
program? I have had good luck with writing the file to a temporary
location, usually with a dot on the front, then executing a move from
that temp location to the real name. A move is usually a single action
(at least that's my understanding) and for most filesystems is merely a
single inode update.  Then just have the directory watcher skip dot
files... Though the lock would probably be better...

http://danconia.org

--
Boycott the Sugar Bowl! You couldn't pay me to watch that game.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to