Copy or Move problem

[LEFT]Hi All,
I have a simple shell script to move .txt file(s) from a source directory one after another to the destination. The source directory is shared to windows using samba. The source files are arriving continuously and approx size is 10-15 KB. Some time the file size reached upto 100 KB and than the problem starts. The moving script is moving an incomplete file. Actually the file moving program is a continuous daemon which starts moving file immediately it appeared in the source. I have tried a sleep 2 before moving, but it's also failed in case the file is some more bigger than 100 KB. :confused:
Now I wanted to know is there any mechanism available to check if the source file is not in use by any other process? It means, I need to move or copy the file only when its being released by its producer process. It might be easier in Unix using ps command, but I don't know how to check it on a samba shared folder.
Can any body among you please give me a hand please?
Regards. [/LEFT]

Use the fuser command to check the status.

fuser file

If the file is busy, you get some output(processes using the file). else back to command prompt.

Guru.

Hi guruprasadpr,

I have opened the 119703559.HL7 in another terminal and tried the one you have suggested in another terminal, but its not giving me any info.

12 [shivalpl1] dbadm % /usr/sbin/fuser 119703559.HL7
119703559.HL7:

I have tried the same after exiting from vi mode, but both time the out put is same.

13 [shivalpl1] dbadm % !!
/usr/sbin/fuser 119703559.HL7
119703559.HL7:
14 [shivalpl1] dbadm %

Regards

The way your script/Application is working is not recommended. In such cases, an another directory ( say "incomplete" )must be created which would be used to transfer the file from the first application and after the transfer is finished, the application itself should move the file to the "complete". your script must pick the file from the "complete" directory.

Hi Anchal,

I have already tried the same. Still the incomplete file moving while the external application is moving the file to the source of my shell script. I think to check if the file is being used by any other process is the best option to choose. But unsure how to user "fuser" command here.
Regards.

vi does not keep the file open - you actually work on a hidden file. Use something like less to simulate an open file.

1 Like

WAH !!!!

Great Cero. Its worked for me. But couldn't store the pid in a valiable. Actually I wanted to check the length of the pid and if its more than 1 than sleep 5 or continue. I am using bash script here.
Please see if you can post a 2-3 line of examples here.

Regards

You can use the returncode of fuser, no need to parse the pid.

while fuser /path/to/your/file >/dev/null; do
   sleep 5
done

Hi Cero,

I have tried your advice, but the program is still falling into the while loop while the file is not running... See the following. I guess, the path to my program returned by fuser may causing the problem with the while loop.

108 [shivalpl1] dbadm % fuser /home/dbadm/myprog
/home/dbadm/myprog:

109 [shivalpl1] dbadm % prog
/home/dbadm/myprog:
Still running
/home/dbadm/myprog:
Still running
/home/dbadm/myprog:
^C

Regards

Strange. Is /home/dbadm/myprog the file beeing accessed?
What does echo $? show right after executing fuser on the file?
Also add set -x at the beginning of your script to see what happens.