Insure the file is not be wringting
xuepengduan
created: 2006-01-02 05:25:36
I write a cron job to download a DVD iso file (about 2G) from a ftp site every day. But the creat of the iso file need a lot of time so I need to check the file is integrated. The meothod I use is that check the size of the file and sleep 10s then check the size of the file secondly. If the size of the file is not changed, the iso file is integrated. I feel my method is not elegant. Is there better ways to solve the program?
Thanks!
Re: Insure the file is not be wringting
created: 2006-01-02 05:30:27

Your method is one way. I would take a different way and have the .iso creation process also create a second file, .iso.ok, which tells you that the .iso has been created completely. Another way would be to create the .iso in a second directory and then rename it into the pickup directory.

Re: Insure the file is not be wringting
created: 2006-01-02 05:54:13

If you also have an ssh or telnet login to the remote site you could use the UNIX lsof command to see whether the file is open for writing by a specific process. For example, say your file is /tmp/test.iso, you could run

lsof /tmp/test.iso
Which would give something like this:
COMMAND   PID USER   FD   TYPE DEVICE SIZE NODE NAME
isowrite 12533 root  3w   REG  254,1    0  235 /tmp/test.iso

The FD entry shows you that the file is opened for writing by the isowrite process.

Not the most elegant method either ([Corion]s solution is better, but requires that you have some control over the iso-creating process), but more reliable than checking for size change.


A computer is a state machine. Threads are for people who can't program state machines. -- Alan Cox
Reaped: Re: Insure the file is not be wringting
created: 2006-01-02 13:01:15
This node was taken out by the NodeReaper on 2006-01-02 14-12-38
Reason: [jdporter]: reap. more troll trash.

You may view the original node and the consideration vote tally.

Re: Insure the file is not be wringting
created: 2006-01-02 14:06:45
Bear in mind that it is possible for file data to change without the file size changing; for example, some data fields in the initial "root blocks" of the iso file might be filled in as a final step, after the full extent of data files have been written. So rather than checking file size, it might be better to check the modification time (using -M $isofilename ).

But as others have pointed out, the best solution is to make a slight change in the process that creates the iso file: the directory or name of the file being created should be different from the directory or name used to download the file; just add a step after the "mkisofs" or whatever, to rename/move the completed file so that the download process will only find it after it's finished.

perlmonks.org content © perlmonks.org and Corion, graff, NodeReaper, tirwhan, xuepengduan

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03