1834387 Members
2293 Online
110066 Solutions
New Discussion

About copy file

 
SOLVED
Go to solution
peterchu
Super Advisor

About copy file

We have two batch jobs , one batch job is rcp a file from remote site to /tmp , and another batch job is to move this file to other directory , but I found that sometimes the second batch job (move file) is processing while it is still copying from remote site , therefore , the file is not complete , could suggest is the best way to solve it ? thx.
11 REPLIES 11
Patrick Wallek
Honored Contributor
Solution

Re: About copy file

The best way would be to use something like fuser or lsof to make sure there are not any processes with the file open.

In your script do an 'fuser -u filename' and if anything is returned then sleep for a period of time and try again.
Naveej.K.A
Honored Contributor

Re: About copy file

hi,
how about putting both the commands in the same job???

with best wishes
Naveej
practice makes a man perfect!!!
peterchu
Super Advisor

Re: About copy file

thx replies, it can't put to the same batch job because the permission reason .
curt larson_1
Honored Contributor

Re: About copy file

start second job later

check for the file size in second job, if the file size hasn't increased in last 20 seconds, it is probably done being copied

have first job creates a file once the rcp is finished saying it is done, second job doesn't copy file till rcp done file exists.
peterchu
Super Advisor

Re: About copy file

thx suggestion , do you have the sample script for my reference .

thx
Kiyoshi Miyake
Frequent Advisor

Re: About copy file

Hi,

simple resolution. you make lock file.
example:

---rcp.sh---
touch /tmp/during_rcp
rcp foo bar
rm /tmp/during_rcp
---

---mv.sh---
while [ -f /tmp/during_rcp ]
do
sleep 300
done
mv bar baz
---

or:
use lockf(2) ( see man page lockf ).
or:
chmod -r ...

Thanks.
KapilRaj
Honored Contributor

Re: About copy file

In my organisation we call it a special resource .. but in a way it will help you in this case as well .. this is about creating a common lock / special resource which enable the script to do it's task.

Something like script1 and script2 starts with ..
************
while [ -f /var/tmp/SPR_REMOTE_CP ]
do
echo Lock file present >> $LOGFILE
sleep 60
done
touch /var/tmp/SPR_REMOTE_CP #Taken exclusive access

rcp or mv bla bla ........ #Do the task

rm /var/tmp/SPR_REMOTE # Removes the lock

Regds,

Kaps
Nothing is impossible
Bharat Katkar
Honored Contributor

Re: About copy file

Peterchu,
What i think is before using mv command check for the existing of the file first and if it exists then only try moving it. See if this works for you.

while true
do
if [ -f /tmp/filename ]
then
mv /tmp/filename /anotherdir
exit
else
sleep 10
fi
done


You need to know a lot to actually know how little you know
Bharat Katkar
Honored Contributor

Re: About copy file

Little clearance:
I have used "true" and "exit" that may not be suitable in your script so modify accordingly to your requirement.
This is just an example.
You need to know a lot to actually know how little you know
peterchu
Super Advisor

Re: About copy file

thx all replies , I want to use lock to fix it , could suggest how to write a script to use it ? thx.
Michael D'Aulerio
Regular Advisor

Re: About copy file

Using a lock file to coordinate between the 2 cron jobs is a good idea. We have used them in the past. One caution. Sometimes one of the cron jobs will die and leave the lock file in place. You should have some mechanism to check for stale lock files and remove them.
Email: michael.n.daulerio@lmco.com