Operating System - HP-UX
1834087 Members
2473 Online
110063 Solutions
New Discussion

Re: Script Help - Coping and Moving Files

 
SOLVED
Go to solution
Laurie_2
Advisor

Script Help - Coping and Moving Files

Hi HP Techies,

I have a technical question concerning a script. I want to copy and move files from one directory to another, but not if the files are being uploaded at the time. That would be a bad thing.

So, I need to add a piece that can read the file size, wait for 3-5 seconds,
read the file size again and then loop if they're not the same, which will wait another 3-5 seconds. Once the files are the same size, Then continue with the script.

Attached is a copy of my 1 page script.

Thank you for your help,
Laurie
lauriek@mail.park.edu
How can you make the world a better place
9 REPLIES 9
MANOJ SRIVASTAVA
Honored Contributor
Solution

Re: Script Help - Coping and Moving Files

Hi Laurie


You need to put a test condition in the script
which goes like this :


while true

do

export SIZE=`ls -l filename | awk '{print $7}'`

if [ $SIZE = < expected size > ]

then

(command to be run)

exit

fi

sleep 90

done


where file name is the name of the file which is expected to reach the , you can also put a greater size or lesse than size condition.


Manoj Srivastava
A. Clay Stephenson
Acclaimed Contributor

Re: Script Help - Coping and Moving Files

Hi Laurie:

Let me give you a somewhat better way to do this. Call my fileage.pl script. It will silently report if a file has been modified within a given period of time. It exits with a 0 status if not and a status of 1 if the file has been modified. This is better than looking at file sizes. I would also look at a bit longer time period than 3-5 seconds.

In your script, do something like this:

SECONDS=30
fileage.pl -m -s ${SECONDS} ${YOURFILE}
STAT=$?
if [ ${STAT} -eq 0 ]
then
echo "File ${YOURFILE} not been modified in"
echo "${SECONDS}. It is safe to transfer."
fi

You can execute fileage.pl -u for full usage.

If it ain't broke, I can fix that.
Craig Rants
Honored Contributor

Re: Script Help - Coping and Moving Files

A few changes I would make

for FILE in `ls dir`
do
SIZE1=`ll $FILE | awk '{print $7}'`
sleep 5
SIZE2=`ll $FILE | awk '{print $7}'`
if [ $SIZE2 -eq $SIZE1 ]
then
execute command
else
don't execute
fi
done

GL,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
H.Merijn Brand (procura
Honored Contributor

Re: Script Help - Coping and Moving Files

Untested:

#!/opt/perl/bin/perl -w

# copycheck sourcedir destdir

use strict;

my ($sourcedir, $destdir) = @ARGV;

use File::Find;
use File::Copy;

find (sub {
$^T = time; # Now is now
-M < 0.02 and return;# File too new
copy $_, $destdir/$_";
}, $sourcedir);
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: Script Help - Coping and Moving Files

Sorry, tested, missing $_

#!/opt/perl/bin/perl -w

# copycheck sourcedir destdir

use strict;

my ($sourcedir, $destdir) = @ARGV;

use File::Find;
use File::Copy;

find (sub {
$^T = time; # Now is now
-M $_ < 0.02 and return;# File too new
copy $_, $destdir/$_;
}, $sourcedir);
Enjoy, Have FUN! H.Merijn
Ruediger Noack
Valued Contributor

Re: Script Help - Coping and Moving Files

I suggest you check if the file is open. Means the file isn't open if the download is completed.
Look at this sample script:

OPEN=`fuser ${YOUR_FILE} 2>/dev/null`
while [ "$OPEN" != "" ]
do
sleep 1
OPEN=`fuser ${YOUR_FILE} 2>/dev/null`
done

Hope this helps
Ruediger


H.Merijn Brand (procura
Honored Contributor

Re: Script Help - Coping and Moving Files

Ruediger, a *very* good point, but remember that by default fuser cannot be invoked by other than the root user. (probably because of the -k option :) and the access to kernel memory). If you live in a secure environment where you trust everyone, you can open up execution rights to fuser, or write a small wrapper with setuit-root that only allows fuser on specific files.
Enjoy, Have FUN! H.Merijn
Ruediger Noack
Valued Contributor

Re: Script Help - Coping and Moving Files

Hi procura,

thanks for your reply.
I know that only root can get access to the kernel and can run command fuser.
But I found in Lauries script the chown command. Therefore I thought the script will be started from root.

Ruediger
Wodisch
Honored Contributor

Re: Script Help - Coping and Moving Files

Hello Laurie,

are you certain that you do NOT want any checks wether the file to be copied/moved does not already EXIST in the target directory (deleting the existing one!)???

Just my $0.02,
Wodisch