witty
asked on
How to be sure, file is written completely
Hi!
I do a check, if a file exists "test -f <file>".
When it exists I do some processing ...
- but how can I be sure, that the file ist completely written, the moment I start the process?
thx
michi
I do a check, if a file exists "test -f <file>".
When it exists I do some processing ...
- but how can I be sure, that the file ist completely written, the moment I start the process?
thx
michi
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
some systems have same command under name fstat
If your script have write access on this file, try to create write lock on the file.
If you can lock it, it's not open for write anywhere else.
If the other process, which is generating this file is yours as well, then
- Create Lock ( touch /tmp/.lock_logfile )
- Generate file ( ls -R > /tmp/logfile )
- Release the lock ( /bin/rm /tmp/.lock_logfile )
If you can lock it, it's not open for write anywhere else.
If the other process, which is generating this file is yours as well, then
- Create Lock ( touch /tmp/.lock_logfile )
- Generate file ( ls -R > /tmp/logfile )
- Release the lock ( /bin/rm /tmp/.lock_logfile )
> but how can I be sure, that the file ist completely written
do mean to perform this check from within a shell script?
If so, it's not that easy. You need to know the filesystem and its configuration (cacheing in particular)
If you're on NFS too, then somthing like ITcrow suggest might be nbecessary too
do mean to perform this check from within a shell script?
If so, it's not that easy. You need to know the filesystem and its configuration (cacheing in particular)
If you're on NFS too, then somthing like ITcrow suggest might be nbecessary too
Your process and the processes which may write to the file should agree on some protocol for determining when the writing is complete.
Or, you might mv the file to a private place to ensure that it can no longer be written to.
Or, you might mv the file to a private place to ensure that it can no longer be written to.
ASKER
ok - I see there is no general solution for this problem!
I think the best way would be, telling the application to rename the file after finishing writing from a temporary name to the final name!
- but I can't use this solution, because I can't change the application.
so the best way would be using the "fuser"- and "lsof"-commands (I don't have "fstat").
avizit: can you give me a hint how to use the lsof-command? then I will split points between you and tfewster!
thx for all your comments
michi
I think the best way would be, telling the application to rename the file after finishing writing from a temporary name to the final name!
- but I can't use this solution, because I can't change the application.
so the best way would be using the "fuser"- and "lsof"-commands (I don't have "fstat").
avizit: can you give me a hint how to use the lsof-command? then I will split points between you and tfewster!
thx for all your comments
michi
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
How can I splt points - I only see "Accept Answer", but not "Split Points".
thx
michi
thx
michi
There is a "Split Points" option just above the "add a comment" box
See https://www.experts-exchange.com/help.jsp#hi69 for more info on splitting points
See https://www.experts-exchange.com/help.jsp#hi69 for more info on splitting points
but that still doesnt gurantee if a file is completely written .. files especially the log files etc can get written any time depending of how some program runs and the errors it comes across ( just to give you an example )