Operating System - HP-UX
1753979 Members
5707 Online
108811 Solutions
New Discussion юеВ

Re: script - check file before copy

 
SOLVED
Go to solution
Ahmed_58
Regular Advisor

script - check file before copy

Hi,
below script transfer file, the file creates by application and start writting data to it and it takes 1 hour to complete, and every two hour create new file, the problem I have I need to copy the file only when it is complete written to it or if it is not 0 size.
--------my script ---------------------
#
cd /NRT/NRT_src
ls *NR_BHRBT_* | read LINE
if [ $? = 0 ]
then
mv $LINE /NRT_backup
date | read sdate

cd /NRT_backup
scp $LINE BHRBTF@111.111.11.11:/
if [ $? -eq 0 ];then
date | read edate
echo Finish copy $LINE $sdate >>/NRT_daily_out.log
else
echo Failed copy of $LINE "$edate" >/NRT_faild.log
fi
fi
-----------------------------------
Regards,
Ahmed
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: script - check file before copy

Hi:

You could use 'fuser' to look for any process(es) still using the file and when none, perform your copy:

...
while true do
PIDS=$(fuser ${FILE} 2> /dev/null) #...look for any process using file...

[ -z "${PIDS}" ] && break #...no processes, so OK to continue

sleep 60
done
...

Use the '-s' test for size:

# [ -s ${FILE} ] && echo "size > 0" || echo "size = 0"

Regards!

...JRF...



Steven E. Protter
Exalted Contributor

Re: script - check file before copy

Shalom Ahmed,

See the if -z and/or -f parameter.

http://forums.bsdnexus.com/viewtopic.php?id=1272

http://www.linuxquestions.org/questions/linux-server-73/shell-script-problem.-check-file-already-exists-601105/

http://www.unix.com/shell-programming-scripting/32809-have-shell-script-check-file-exist-before-processing-another-file.html

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Dennis Handly
Acclaimed Contributor

Re: script - check file before copy

>only when it is complete written to it or if it is not 0 size.

The first is hard to determine unless you are root and can use JRF's fuser.

If there is something at the end if the file you can check, you can use tail and grep.
OldSchool
Honored Contributor

Re: script - check file before copy

one other possibility, if you can't use fuser, would be to check the file size (ls -l or ?), wait for a bit then check it again to see if its changed...

but valid values of "wait for a bit" might be hard to determine.....
Steven Schweda
Honored Contributor

Re: script - check file before copy

> [...] the file creates by application [...]

Some applications are more helpful than
others in cases like this. For example, when
Info-ZIP Zip creates an archive (say,
"fred.zip"), it normally uses a temporary
file name (like "ziXXXXXX", with help from
mkstemp()). Then, when the work is done (and
if all went well), it renames the temporary
file to the requested name ("ziXXXXXX" to
"fred.zip", in this case). So, if you can
see "fred.zip", then you can be pretty sure
that it's all there.

Of course, this requires some extra work by
the application.
Ahmed_58
Regular Advisor

Re: script - check file before copy

thanks to all, the script is working now with your help.