LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Copy File While File is Creating in Disk (https://www.linuxquestions.org/questions/linux-general-1/copy-file-while-file-is-creating-in-disk-542826/)

suhas7860 04-02-2007 09:27 PM

Copy File While File is Creating in Disk
 
Hi,
I want to write a c code which will run in cron periodically(every 10 minutes) whose task
is:-
1. Check for the existance of a particaluar file in var suppose /var/core.
2. If the file exist it will copy the file to another location.

Now my file is quite big and when it creats it takes times. Suppose during the time when
that file has been creating in disk if cron called my file copy program then it will sure
find the file and copy it. But that time file creation still not finished so it will copy
a broken file. So how to avoid this?? Can you please suggest any idea.
How to check whether a file creation is finished or not. Is it to check the process
stat for R using ps command ?
Please give your idea.
Regards
suhas

macemoneta 04-02-2007 10:07 PM

What distribution are you running? There are some inotify tools (incron, inotifywait) that make this very trivial, but they require a recent distribution (at least kernel 2.6.13, preferably kernel 2.6.16 or later).

The incron daemon is like cron, except it runs jobs based on filesystem events. You can then run inotifywait to wait until the file is closed, at which point you can safely copy the file. More details here.

suhas7860 04-02-2007 11:14 PM

Re: File Copy while file is creatig
 
Thanks for your effort.
Im am going to use Linux 2.6.17 so I think ur solution will
work fine.
Now I am testing this in my Fedora 5 X86_64
My kernel fedora is
2.6.19-1.2288.2.4.fc5
Regards
Suhas

pixellany 04-03-2007 08:20 AM

Why would you do this in C? All you need is a shell script and the appropriate entries in crontab....

theYinYeti 04-03-2007 08:38 AM

Chances are, that if you move (mv) the file while it is being created, AND move it to a location on the same filesystem, then your file won't be broken. Indeed, AFAIK, file creation occurs related to an inode. Moving the file to another directory of the same filesystem does not change the inode number.

You can have weird things happening with this subtlety, like tail -f waiting on a file that has been rotated...

Yves.


All times are GMT -5. The time now is 03:24 PM.