check if some file is in copy process, then transfer it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting check if some file is in copy process, then transfer it
# 1  
Old 12-15-2010
CPU & Memory check if some file is in copy process, then transfer it

my user copy large files, and it's take 10min for file to be copied to the server (/tmp/user/ files/), if in the meantime start my scheduled script, then it will copy a part of some file to server1

my idea is to check the file size twice in a short period (1-2 seconds) of time, then compare, if the size is different then script transfer1.sh will copy it the next time wen is started from cron,

If anyone has an idea how to do this or any other suggestions?

PS another thing, files name will be random, but every file have "TEST" in name.
when my script run there will be about 5 files to transfer, and this control is just for case if one of the files is in copy process, but other files must be transfered to server1

30 * * * * /myscript/transfer1.sh
Code:
#! transfer1.sh  script

cd /tmp/user/files/
for f in `ls *TEST1*` 
#! here i need check if some file is in copy process
do
scp -qB $f user1@server1:/tmp/end/file/$f
rm -rf /tmp/files/$f
done

example, folder contains
/tmp/user/ files/
TEST1.out
TEST2.out
TEST3.out
TEST4.out (example this file is still copying and will bee skiped with transfer to server1)

after transfer delete all files except TEST4.out, that file will bee copyied next time when script is started

Last edited by waso; 12-15-2010 at 05:26 PM..
# 2  
Old 12-15-2010
Bad idea -- how long is long enough? Best idea: write under another name like same.temp and then rename when done. Second choice: use fuser to see if it is open, but even if not open, file may be incomplete due to a failed writer, unless you have a way to validate it, such as a trailer record.

PS: Comment s/b after the do line!
# 3  
Old 12-15-2010
Instead of checking the file size you should check if the file is open. You can use the fuser utility to do that:

Code:
f=/tmp/x.x
test ! -f $f && echo file not found && exit
c=`fuser -f $f 2>&1 | wc -w`
c=`echo $c`
case $c in
1) echo file is not open;;
*) echo file is open;;
esac

Tested with bash on OpenBSD

---------- Post updated at 12:57 PM ---------- Previous update was at 12:54 PM ----------

Quote:
Originally Posted by DGPickett
Bad idea -- how long is long enough? Best idea: write under another name like same.temp and then rename when done. Second choice: use fuser to see if it is open, but even if not open, file may be incomplete due to a failed writer, unless you have a way to validate it, such as a trailer record.

PS: Comment s/b after the do line!
Agreed. Copy/rename is the fool proof way to avoid problems.
# 4  
Old 12-15-2010
Well, some people send additional short ack files, perhaps with a line or byte count or checksum, but that is excess complication if you can rename.
Code:
if [ -f "$f" -a "$( fuser "$f" 2>/dev/null )" = "" ]
then
 # file is not open
fi

Since fuser puts the pids on stdout and the rest (path name, colon, open type indicators) on stderr, you can just test for pids or you can ps -fp the pids to show who is on the file!
# 5  
Old 12-15-2010
another thing, files name will be random, but every file have "TEST" in name.

when my script run there will be about 5 files to transfer, and this control is just for case if one of the files is in copy process, but other files must be transfered to server1
# 6  
Old 12-17-2010
So, did you pick a robust method of dealing with partally completed files?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If statement to check file transfer

Hello Guys, I am trying scp few file within if statement, but getting error, can someone please help to understand, what mistake I am making ? if && ] ; then echo " Files transferred to Log servers successfully. " else echo " One or more file transfer failed over... (10 Replies)
Discussion started by: UnknownGuy
10 Replies

2. UNIX for Dummies Questions & Answers

Issues in Csv file transfer copy from one dir to another

Hi Unix community, I got this code from you guys and I tried to modify it to use for my csv dir transfer basically i want the .csv file to copy itself and populate it to the archive dir. #!/bin/ksh dir1="/home/pumela/unixtestprod" cd "$dir1" echo "code is running" for srcd in... (13 Replies)
Discussion started by: phumaree
13 Replies

3. UNIX for Dummies Questions & Answers

Ftp - file transfer and check successful delivery

In shell script, I want to transfer files continuously and make sure transfer is successful. Please advise... how to make sure ftp transfer is successful? Also is there any option such as sftp -b where I can pass multiple put <file name> commands to ftp Thanks! (1 Reply)
Discussion started by: vegasluxor
1 Replies

4. Shell Programming and Scripting

Automation, Copy, File Status Check --> Script(s)

All, I have to write a script to do the following requirement. There is a file called BUSINESS_DATE.TXT. This file get updated once the oracle partition created. In Oracle, Partition will be created every day. There is a seperate script scheduled to take care ORACLE partition creation. The... (3 Replies)
Discussion started by: karthi_mrkg
3 Replies

5. UNIX for Advanced & Expert Users

Secure Copy - File Transfer between 2 server

Using RCP command we can transfer file from one server to another server. While transferring we can rename the file also e.g. File name = FILE123.txt (lying on Source server = oldserver) Target Server Name = newyour Renamed File = FILE456.txt rcp FILE123.txt newyour:./FILE456.txt... (1 Reply)
Discussion started by: Pash
1 Replies

6. UNIX for Advanced & Expert Users

How to copy a binary file while the file is being written to by another process

Hello, Can I copy a binary file while the file is being written to by another process? Another process (program) “P1” creates and opens (for writing) binary file “ABC” on local disk. Process P1 continuously write into ABC file every couple of seconds, adding 512-byte blocks of data. ABC file... (1 Reply)
Discussion started by: mbuki
1 Replies

7. Shell Programming and Scripting

check if file finished to copy

Hi all, I have a script that is monitoring a hot folder. This script works fine with one exception when the script is executed while a file is being copied to the hot folder. What is the easiest method to check if the copy file is completed? I'd like to get the solution in bash :) (8 Replies)
Discussion started by: gigagigosu
8 Replies

8. Shell Programming and Scripting

Copy Files to Dir and Check If File Exists

Hi everyone. I am trying to write a bash script that will copy files from one directory to another but I need to be able to check the directory that I'm copying the files to and see if the file already exists. If it does I need to add a number at the end of the copied file. Thanks for your help. (3 Replies)
Discussion started by: snag49ers
3 Replies

9. Shell Programming and Scripting

How to check a file exist and do a copy of other files

Hi, I would like to perform bash which would check the file A.txt to be size 0 or not. If the size is 0, I would copy file B.txt to replace A.txt. Please help. Thanks. -Jason (6 Replies)
Discussion started by: ahjiefreak
6 Replies

10. Programming

transfer an open file description between two process

How can I transfer an open file description between two process? (1 Reply)
Discussion started by: xu_wen_dong
1 Replies
Login or Register to Ask a Question