Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 04-02-2007, 09:27 PM   #1
LQ Newbie
Registered: Mar 2007
Posts: 6

Rep: Reputation: 0
Copy File While File is Creating in Disk

I want to write a c code which will run in cron periodically(every 10 minutes) whose task
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.
Old 04-02-2007, 10:07 PM   #2
Senior Member
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
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.
Old 04-02-2007, 11:14 PM   #3
LQ Newbie
Registered: Mar 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Thumbs up 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
Old 04-03-2007, 08:20 AM   #4
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Why would you do this in C? All you need is a shell script and the appropriate entries in crontab....
Old 04-03-2007, 08:38 AM   #5
Senior Member
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 63
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...



copy, file

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
copy a file to the flash disk and extract it from the flash disk indu.a Linux - Kernel 2 11-15-2006 03:22 AM
copy vid tape to file and burn file to DVD jim mann Linux - Software 4 01-24-2006 09:57 PM
Is arrangement of file systems will differ if we copy a file from FAT 32 to ext 3 ? anindyanuri Linux - Software 2 02-20-2005 11:39 AM
Creating an EXACT copy of a disk elluva Linux - Software 5 10-14-2004 11:21 PM
how to copy file in linux to floppy disk aiyong Linux - Newbie 3 10-17-2003 05:26 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:59 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration