It has already been asked here and I have found the solution for the first part of my question. So for the moment I have a bash script (script1) where I evaluate the sha1sum of a specific file (output.dat) periodically (lets say every 1 sec). When the sha1sum_cur != sha1sum_prev then I run another script (script2) with some commands. Meaning this way that the file has modified.

My problem is that output.dat is quite a lot MBs and takes quite long to be written. So, I dont want to run script2 while data are being written to it, but after the process of writing data to it has finished. output.dat is being created by a fortran code, so lets say every 1h it deletes previous data and writes new ones but takes around 5min for that.

two last important notes: 1) Since I am submitting my jobs to a cluster, i dont have the permission to install inotify-tools. 2) I could call the script through the fortran code, once I close(output.txt) with CALL system(./script2) but I would rather not do so, since its parallel and don't want to slow down its efficiency.

So the question is, what I could add to script1 so that it understands that the process of writing data to output.dat has finished?

  • You don't need to install inotify-tools into the system, you can just build it in you home directory and run it from this place. – Ilay Nov 27 '14 at 12:25
  • Indeed llay that can be done, i was thinking though of a more portable script, not depending on other tools/libraries. – AlphaBeta Nov 27 '14 at 14:34

You could use lsof to check if any process is accessing that file. However, this would not protect against other process changing the file after you've checked for access in you shell script.

To avoid race conditions, I'd use atomic file operations, like mv, to mark that your shell script or other software has not 'picked up' the file. This is a kernel call and is guaranteed to be atomic (as in, if two processes call mv on the same file at the same time, one will succeed and the other will fail).

Another possibility is to use an external locking tool like lockfile-progs, but those you can't use from you software that drop the file. mv is universal, although an arguably dirty solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.