knowing when a file is busy?

knowing when a file is busy?

Post by joe mc coo » Wed, 08 Nov 2000 04:00:00



SCO OSE 5.05 and Unixware.

This is a general question: let's say that I have a flat file that is
being written to occassionally by an application (I have a single user
dos application in mind) and let's say that I want to create a cron
job that will grab that file and send it to a printer, perhaps with a
little formatting.

How can I be sure that the application has finished writing to the end
of the file before I grab it ?  Cron would check every minute, say,
and if the file existed it would send it to the printer and then erase
it.  Of course I could rename the file before printing it.  But does
that guarantee that I won't loose data ?  What is the safe approach ?
--
joe mc cool SMIEEE
========================================================================
Tangent Computer Research BT71 7LN (www.tangent-research.com)
voice:(44)2837-548074fax:(44)-870-0520185 The more you say the less the better.

 
 
 

knowing when a file is busy?

Post by Tony Lawrenc » Wed, 08 Nov 2000 04:00:00



> SCO OSE 5.05 and Unixware.

> This is a general question: let's say that I have a flat file that is
> being written to occassionally by an application (I have a single user
> dos application in mind) and let's say that I want to create a cron
> job that will grab that file and send it to a printer, perhaps with a
> little formatting.

> How can I be sure that the application has finished writing to the end
> of the file before I grab it ?  Cron would check every minute, say,
> and if the file existed it would send it to the printer and then erase
> it.  Of course I could rename the file before printing it.  But does
> that guarantee that I won't loose data ?  What is the safe approach ?

If the dos app closes the file, you can use a named pipe as we do
to make network printers into device files (
http://aplawrence.com/SCOFAQ/scotec7.html#netdevice ).

If it has to be a real file, you can use fuser to see if any
process is currently writing to the file, but that still puts you
in a race condition if you can't control the Dos app.  There's
really no good way around that, though you can improve your
chances by giving the dos app a very low priority.  You could
(with some effort, of course) write your side as a driver and
make the usage/copy check atomic, but that could drag down the
rest of the system and it doesn't sound like a great idea even on
paper :-)

Of course, if you can control the dos app, even by wrapping it in
a script that will set directory locks (making a directory is
atomic, so you can make crude locks by observing the failure to
create a directory because it exists), then you have no problems
at all.

--

SCO/Linux articles, help, book reviews, tests,
job listings and more : http://www.pcunix.com

 
 
 

knowing when a file is busy?

Post by Tony Lawrenc » Wed, 08 Nov 2000 04:00:00



> SCO OSE 5.05 and Unixware.

> This is a general question: let's say that I have a flat file that is
> being written to occassionally by an application (I have a single user
> dos application in mind) and let's say that I want to create a cron
> job that will grab that file and send it to a printer, perhaps with a
> little formatting.

> How can I be sure that the application has finished writing to the end
> of the file before I grab it ?  Cron would check every minute, say,
> and if the file existed it would send it to the printer and then erase
> it.  Of course I could rename the file before printing it.  But does
> that guarantee that I won't loose data ?  What is the safe approach ?

I just remembered something else I did once that might work for
you, depending on what you are doing:

In the case I had, it didn't really matter if everything got done
this time, just so long as we picked up at the right place next
time.  So the file just kept growing and growing, and I'd take
the parts that hadn't been seen yet and bundle 'em off to the
other app every 15 minutes or so. A simple line counter was all I
needed.  Once a week or so the writing app would be stopped
during off-hours and we'd prune the file and start over again..

--

SCO/Linux articles, help, book reviews, tests,
job listings and more : http://www.pcunix.com

 
 
 

knowing when a file is busy?

Post by Fabio Giannott » Wed, 08 Nov 2000 04:00:00




Quote:> SCO OSE 5.05 and Unixware.

> This is a general question: let's say that I have a flat file that is
> being written to occassionally by an application (I have a single user
> dos application in mind) and let's say that I want to create a cron
> job that will grab that file and send it to a printer, perhaps with a
> little formatting.

> How can I be sure that the application has finished writing to the end
> of the file before I grab it ?  Cron would check every minute, say,
> and if the file existed it would send it to the printer and then erase
> it.  Of course I could rename the file before printing it.  But does
> that guarantee that I won't loose data ?  What is the safe approach ?
> --
> joe mc cool SMIEEE
> ========================================================================
> Tangent Computer Research BT71 7LN (www.tangent-research.com)
> voice:(44)2837-548074fax:(44)-870-0520185 The more you say the less the
better.

An excellent question.  I would be very interested to know the answer to
this.  (The concept would also apply to large files going to the print or
email spooler, or even to system log files...)
 
 
 

knowing when a file is busy?

Post by John DuBo » Thu, 09 Nov 2000 13:35:21




Quote:>How can I be sure that the application has finished writing to the end
>of the file before I grab it ?  Cron would check every minute, say,
>and if the file existed it would send it to the printer and then erase
>it.  Of course I could rename the file before printing it.  But does
>that guarantee that I won't loose data ?  What is the safe approach ?

You need a combination of approaches.  First, rename the file; then, use fuser
or lsof in a loop to check whether the file is open.  Open file descriptors
won't be affected by the renaming, so if anything is writing to the file it
will continue writing.  Once the file is no longer open, print it and remove it
(arrange for both at once with lp -R).  Note that you need to be root to use
fuser.

        John
--

 
 
 

1. I am a Linux Know Nothing, would like to know

you can shirnk your windows 98 partition and use the freed up space for
linux, then dual boot between the two (choose which to boot).. try
TurboLinux, www.turbolinux.com/orders/ you can grab the boxed set w/ manual
that walks you through every step of installing it, including shrinking your
partition, it also includes WordPerfect8 for linux.. you can also download
it via ftp and cheapbytes (www.cheapbytes.com) will have it soon as well,
though you won't get the book and probably won't get WP8.. as fors
creenshots, there are tons of places on the web you can find screenshots of
people's systems
-Justin

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Justin Ryan

Internet/Developer Relations Associate
Pacific HiTech / TurboLinux
http://www.turbolinux.com/

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2. Remote debugging

3. Reading a directory and knowing which type of file am I reading

4. Drivers for D-Link 10/100 cards

5. Sorry, I am too busy for the FAQ.

6. FreeBSD & Apache

7. Dont know what I am looking at...

8. adding new driver to kernel - not module

9. How to know what shell I am ruuning from a C program?

10. Why I am glad I know perl

11. "Do you know where I am ??"

12. Error When Compiling - Don't Know What I Am Missing

13. How do I know which window manager I am using?