0

I need to know if there is a way to know if a file in a shared disk is in use by another Linux instance.

I have 2 Linux machines sharing a disk. Each random time, the first machine writes a results file (with consecutive filename) to the shared disk when an analysis process is done.

In the other machine I have a bash script verifying if the file has already been finished by the first machine.

The way I verify now is in a for loop in bash script running the stat command to know if the Last modified date of the file is after the current date of the machine. If this is true I can process the file. If not, I run a sleep and then I run stat again.

So, there is any way to avoid this and to know if the file in the shared disk is in use by another machine? Or which is the best way to wait for the finished file?

Thanks in advance.

Hermandroid
  • 2,120
  • 4
  • 29
  • 35

3 Answers3

2

Write the result file into the same directory with a temporary name. Only rename it to its final name after closing the file under its temporary name, ensuring that contents are flushed. Thus, if the file exists under its final name, it is guaranteed to be complete.

This needs to be into the same directory because NFS renames are only guaranteed to be atomic within a single directory (whereas in non-NFS scenarios, a location anywhere on the same filesystem would work).

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Thanks CharlesDuffy that's a good idea but the problem is that I have no access to the machine which writes the file :'(. – Hermandroid Jul 23 '14 at 19:06
  • ...well -- if you have no access to that machine, you can't force it to use flock-style advisory locking either, so that's two suggestions that won't work. – Charles Duffy Jul 23 '14 at 19:12
  • @Herman Can you not even ssh into the machine writing to the file from your machine? – SeriousBusiness Jul 23 '14 at 19:28
  • The solution is simple as to add a lock file. While I'm using the file I can create a lock file, if this lock file exists, I proceed to wait until this lock file dissapears. – Hermandroid Aug 11 '14 at 23:45
  • Sounds good. Be sure to use `mount -o sync` for the NFS mount if your platform is Linux; while _in theory_ `close()` will perform a COMMIT operation, I've seen questions here on StackOverflow stemming from places where asserting that process A completed its exit before relying on the things it wrote to be completely flushed to the NFS server was not in fact adequate. – Charles Duffy Aug 12 '14 at 12:05
0

You can try using flock as demonstrated here: How to prevent a script from running simultaneously?. There are other ways to synchronize file access as well (lockfile, mkdir, just to name a few). A simple google search should give you what you need. I'm not 100% sure this if appropriate for your setup with the shared disk though.

Community
  • 1
  • 1
SeriousBusiness
  • 1,162
  • 8
  • 9
  • Short form is that whether advisory locking will work across a network filesystem depends on the specific network filesystem implementation. For a SAN, GFS2 supports it properly; NFS implementations, by contrast, tend to vary. – Charles Duffy Jul 23 '14 at 18:52
  • Thanks I'm going to check this. – Hermandroid Jul 23 '14 at 19:07
0

Kind of the same approach as comparing the date:

Get the filesize two time and compare

STAT1=`du [FILE] | awk '{print $1}'`;
STAT2=`du [FILE] | awk '{print $1}'`;
[ $STAT1 -ne $STAT2 ] \
    && echo "writing to..." \
    || echo "FINISHED"

If you expect hevy I/O and iowaits put a sleep 1 between the to STATS.

jxp315
  • 108
  • 1
  • 6