Operating System - HP-UX
1826703 Members
2724 Online
109696 Solutions
New Discussion

file active during ftp being processed.

 
Ferdie Castro
Advisor

file active during ftp being processed.

Hi I made a script to mv some files from /dir1 to /dir2 to freeup diskspace for /dir1 which in increasing because other servers are dumping file to it via ftp.
My problem is how can I edit my script such that I cannot mv a file from /dir1 to /dir2 while at the same time it is being dumped via ftp from other servers to /dir1. Thanks in advance.

Ferdie
7 REPLIES 7
Christopher Caldwell
Honored Contributor

Re: file active during ftp being processed.



Use a tmp file as a semaphore, or if your app supports it, use file locking.
Steven E. Protter
Exalted Contributor

Re: file active during ftp being processed.

filename="schmobagel.dat"

if [ -f $filename ]
then
echo "not coing to copy $filename"
else
cp /dir1/$filename /dir2
fi

This concept will prevent you from overriting files that are in process of moving. Add it to your script.

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
Sanjay Kumar Suri
Honored Contributor

Re: file active during ftp being processed.

Hello

1. In the begining store the output of ls dir1 to a temporary file.

2. For each file in the above temporary file use the mv to do the needful.

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Mark Grant
Honored Contributor

Re: file active during ftp being processed.

I'm not sure I really understand SEP's solution here so it may well be the best idea. SEP's often are :)

Other than that, I'm not sure of a foolproof way you can do this so my suggestion is to just not mv the latest file in the directory as in

#!/usr/bin/sh
shift 1
for $file in $#
do
mv $file /dir2/$file
done

Call this script, eg. filemove.sh and run it from /dir1 with

filemove.sh `ls -t`

A bit messy though.
Never preceed any demonstration with anything more predictive than "watch this"
Alan Turner
Regular Advisor

Re: file active during ftp being processed.

FTP is pretty fast, so you could reasonably expect that, if the file modification time hasn't changed in, say, 120 seconds, then the remote server has finished FTPing the file:

DELAY=120
PATH=/usr/bin
FLAG=/var/tmp/ftp_timestamp
rm -f $FLAG
touch $FLAG
cd /dir1
while true do
sleep $DELAY
for f in *
do
if [ -f "$f" -a "$f" -ot $FLAG ]
then
mv $f /dir2
fi
done
touch $FLAG
done
Bernhard Mueller
Honored Contributor

Re: file active during ftp being processed.

Ferdie,

I guess you are save to mv when no process is accessing the file.

So I would set it up like that

targetdir=/target
sourcedir=/source
cd $sourcdir
# only files in that dir
for i in `ll | grep "^-" | awk '{print $9}'`
do
# only if no process is using them
if [ `fuser -u $i 2> /dev/null | grep -v "^$" | wc -l` -eq 0 ] ; then
mv $i $targetdir
fi
done

Regards,
Bernhard
vasundhara
Frequent Advisor

Re: file active during ftp being processed.

Hi,

I agree with what ever Bernhard has suggested. fuser is the best way to find if any process is using the file.

fuser displays the user name if any process is using that file. So, the solution given by Bernhard is much suitable for your requirement.

Regards
VJ.